MysQL(表 1):
+----+--------+-------+--------+
| id | itemid | title | status |
+----+--------+-------+--------+
| 1 | 2 | title | 0 |
+----+--------+-------+--------+
| 2 | 2 | title | 1 |
+----+--------+-------+--------+
| 3 | 3 | title | 1 |
+----+--------+-------+--------+
| 4 | 3 | title | 0 |
+----+--------+-------+--------+
| 5 | 3 | title | 0 |
+----+--------+-------+--------+
MySQL(表 2):
+----+---+---+
| id | x | y |
+----+---+---+
| id | 1 | 2 |
+----+---+---+
PHP:
(我知道下面的查询没有意义,但它应该只是说明我在这里要做什么。)
$a = mysql_query("SELECT t1.title FROM table1 AS t1 WHERE t1.title = 'title' ...IF t1.status = 1 THEN (SELECT * FROM table2 AS t2 WHERE t2.x = '1' AND t2.y = t1.itemid) ORDER BY `id`");
while($b = mysql_fetch_assoc($a))
{
echo $b['title'];
}
所以,我想做的是:
1) 从table1
中获取所有匹配title = title
2) 如果table1
中的status
等于0
什么都不做,只显示数据
3) 然而,如果 table1
中的 status
等于 1
那么它应该检查 中是否有记录table2
其中 x = 1
和 y = itemid(来自 table1)
,如果没有,则来自 table1
的数据应该被排除
示例: 在上面的例子中,应该显示ids (table1): 1, 2, 4, 5 ...3应该被排除,因为status是1,表2中没有匹配的记录。
我希望这是有道理的:/
最佳答案
关于php - 如何在 MySQL 中执行 IF THEN 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5922865/