Php 在mysql中,如果if()函数返回FALSE,如何设置WHERE条件;如果if()函数返回TRUE,如何设置END query条件

Php 在mysql中,如果if()函数返回FALSE,如何设置WHERE条件;如果if()函数返回TRUE,如何设置END query条件,php,mysql,Php,Mysql,我想使用mysql检查mysql数据库中是否存在值。如果该值存在,我不想做任何事情(我不想获取任何数据)。如果它不存在,那么我想设置一些where条件 到目前为止,这就是我所知道的,但这是不对的。因为我仍然获取数据,如果没有,其中设置了条件 SELECT *, CASE WHEN ( table_name.record = 'inputrecord') THEN //Do nothing because it is found already ELSE // since inp

我想使用mysql检查mysql数据库中是否存在值。如果该值存在,我不想做任何事情(我不想获取任何数据)。如果它不存在,那么我想设置一些
where
条件

到目前为止,这就是我所知道的,但这是不对的。因为我仍然获取数据,如果没有
,其中设置了
条件

SELECT *,
CASE
WHEN ( table_name.record = 'inputrecord')
THEN
    //Do nothing because it is found already
ELSE 
    // since inputrecord does not exist, we will start looking for 'id'
    ( WHERE table_name.id = '123')
END                 
FROM table_name
注意:在上述示例中,我编写了
何时(table_name.record='inputrecord')
而不是
何时(table_name.record!='inputrecord')
。这是因为我只想在数据不在表中时继续查询

如果函数存在函数的话,使用
可能更好,但我不知道如何操作

任何帮助都会很好。现在我遇到错误

试试这个

SELECT *
CASE
 WHEN table_name.record = 'inputrecord' THEN 'Unspecified'
 WHEN table_name.id = '123' THEN 'table by id'
END
FROM table_name;
选择*
从表\u名称
当表_name.record'inputrecord'时的情况
然后表_name.id='123'
否则1=1
结束
您可以在代码中应用给定的条件

  • 当输入条件不匹配时,应用过滤条件

  • 当输入条件匹配(否则)时,检索所需结果


  • SQL不是这样工作的

    SQL总是返回行的集合。它可能为空,有时行可能包含空值。您必须计算出筛选集合的条件

    一个通用示例:例如,我们有一个包含列(型号、颜色、年份)的汽车表,您希望在表中查找有关汽车的信息:

    (* Find all red cars *)
    select * from cars where color = 'red'
    
    (* Find all red cars from 1985 *)
    select * from cars where color = 'red' and year = 1985
    
    (* Find all colors which exist both in 1990 and 2000 *)
    select distinct color from cars A where year=1990 and exists (select 1 from cars B where year=2000 and B.color = A.color) 
    
    请确切地告诉我你想要实现什么

    编辑:这应该可以

    (* select a record cars = 'Audi' only if cars = 'BMW' is not found in the whole table. otherwise. I do not want to select Audi even if it exists *)
    
    select * from cars where model = 'Audi' and not exists (select 1 from cars where model = 'BMW')
    

    在这里,如果您的表中有BMW,您将得到0行,否则一个AUDI列表

    您愿意演示吗?如果您在(table_name.record='inputrecord')时什么都不做,那么为什么不尝试像“select*from table_name where table_name.record!='inputrecord'和table_name.id='123'”@M博士,我可以请你重读这个问题吗。我在我的笔记中提到了为什么我不能这么做。为了简单起见,我试图选择一个记录cars='Audi',只有当cars='BMW'在整个表格的记录中找不到时。否则。我不想选择奥迪,即使它存在。非常感谢你的简单和详细的解释。非常好的想法:)谢谢让我试试这个:)谢谢,但我需要的是,如果在整个表中找到一次“inputrecord”,则条件应该返回false,并且应该返回0行。但是,如果在任何时候都没有在表中找到“inputrecord”,那么我想应用过滤器并返回行。您可能已经找到了解决方案,但它会为每一行测试“inputrecord”。因此,如果它存在于前一行中,并对另一行执行另一次检查,它将忽略前一次检查并返回不同的结果。有什么办法可以解决这个问题吗
    (* select a record cars = 'Audi' only if cars = 'BMW' is not found in the whole table. otherwise. I do not want to select Audi even if it exists *)
    
    select * from cars where model = 'Audi' and not exists (select 1 from cars where model = 'BMW')