3

这基本上是我的查询的结构:

SELECT * FROM foo WHERE foo1 = (subquery here)

我想检测子查询何时不返回任何行,然后使用另一个值作为占位符。

我怎样才能做到这一点?

4

4 回答 4

1

你可以使用左连接,它会工作:

SELECT * FROM table AS t1
left join 
table as t2 
on t1.Target = t2.Target
and t2.phase="B"
where t2.target is null OR 
OR t1.date < t2.Date

you can replace your conditions here by changing null ans your conditions here. By doing a left join you are including all rows on the left side of the join, and only matching rows from the right side of the join. Assuming a left side table t1, and right side table t2, in cases where the join condition is not met the value of any column in t2 will be be null. Since the goal in this case is to omit the where clause if our join condition is not met (targets match and the t2 phase value is 'B') we first check to see if the join condition failed, if so we return a row.

于 2013-03-30T05:54:20.977 回答
1

Got the answer!

SELECT * FROM foo WHERE foo1 = IF((subquery with count parameter as replacement), placeholder, (subquery))
于 2013-03-30T07:00:29.733 回答
0

单独尝试subquery,然后使用: mysql_num_rows要知道子查询影响了多少行,如果数字为 0,则使用另一个占位符创建查询

注意 rowCount()要知道使用 PDO 的行数

于 2013-03-30T05:40:29.747 回答
0

您可以通过上面的条件传递它,例如使用 mysql_num_rwos 计算您获取的数据,并将该值分配给一个变量,然后将其放入 if else 循环中以获取如下条件:

$a = mysql_num_rows(query);
if($a == '')
{
$a = your placeholder here;
}
SELECT * FROM foo WHERE foo1 ='".$a."'

类似的东西。

于 2013-03-30T05:43:29.217 回答