简体   繁体   English

有条件地执行查询

[英]Conditionally execute query

Is there a way to execute a query, depending on the result of a certain column? 有没有一种方法可以根据特定列的结果执行查询?

My table has a column col1 , which could be NULL . 我的表有一个列col1 ,它可以为NULL I want to check first. 我想先检查一下。 If col1 is not NULL, execute another query, if col1 is NULL, do nothing (or return something else). 如果col1不为NULL,则执行另一个查询;如果col1为NULL,则不执行任何操作(或返回其他操作)。

In pseudocode it could look like this: 用伪代码看起来可能像这样:

IF (SELECT col1 IS NUT NULL FROM `tab1`)
THEN (SELECT col1, col2, col3 FROM `tab1`)

PS: I execute those queries from PHP, so it would also be possible, to check the result of col1 with PHP, though I would prefer to use plain SQL. PS:我从PHP执行这些查询,因此也可以用PHP检查col1的结果,尽管我更喜欢使用普通的SQL。

the where clause is your if where子句是您的if

  select col1, col2, col3 FROM `tab1`
  where col1 IS NOT NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM