gpt4 book ai didi

MySQL 选择查询值

转载 作者:行者123 更新时间:2023-11-29 21:32:20 25 4
gpt4 key购买 nike

给出下表:

id   name       value
1 primary value1
2 secudary value1
3 primary value1
4 secudary value1
5 primary value3
6 secudary value3

我需要SELECT * FROM table_name WHERE value = "value1"

但是我不能同时从主字段和辅字段中选择信息。

我想检查是否有 SELECT * FROM table_name WHERE value = "value1"and name = "primary" 的记录 如果有记录我使用这个结果,如果没有记录我搜索SELECT * FROM table_name WHERE value = "value1"and name = "secondary"

有什么想法吗?

最佳答案

您需要声明一个变量,选择记录来加载该变量,检查它是否有值并使用 if 语句进行选择。 (我在存储过程中执行此操作)。

declare $found int;
set $found = null;
set $found= (select id FROM table_name WHERE value = "value1" and name = "primary" limit 1);

if ($found is null) then
SELECT * FROM table_name WHERE value = "value1" and name = "secundary";
else
SELECT * FROM table_name WHERE value = "value1" and name = "primary";
end if;

关于MySQL 选择查询值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com