如何将这些查询整合到一个查询中?
好吧,我再试一次……这次我绝对做对了。很抱歉造成混乱。 :(
table1:
+----+---------+------------+------+
| id | item_id | product_id | type |
+----+---------+------------+------+
table2, table3, table4:
+----+---------+
| id | item_id |
+----+---------+
这是我的主要查询:
$sql = "SELECT t1.* FROM table1 AS t1, table2 AS t2 WHERE t2.id = '1' AND
t2.item_id = t1.item_id AND t1.type NOT IN ('type1', 'type2') LIMIT 5";
$a = mysql_query($sql);
while($b = mysql_fetch_assoc($a))
现在我想集成这 2 个查询< /strong>:
第一个查询:
AND IF t1.type = 'type3'
THEN
SELECT t3.item_id FROM table3 AS t3 WHERE t3.id = t1.product_id AND
t3.item_id NOT IN (SELECT t2.item_id FROM table2 AS t2 WHERE t2.id = '1')
仅当此语句t3.item_id NOT IN (...) 是true,则应显示主查询中的
t1.id
,但是,如果t3.item_id is IN (...)
,则应显示。应从结果中排除
第二个查询:
AND IF t1.type = 'type4'
THEN
SELECT t4.item_id FROM table4 AS t4 WHERE t4.id = t1.product_id AND
t4.item_id NOT IN (SELECT t2.item_id FROM table2 AS t2 WHERE t2.id = '1')
第一个查询相同。
与 全部适合一个查询吗?我需要它在一个查询中只是,所以我知道下一页上显示哪些结果,例如 LIMIT 5,5
Okay, I'm going to try it again... this time I definitely got it right. Sorry about the confusion. :(
table1:
+----+---------+------------+------+
| id | item_id | product_id | type |
+----+---------+------------+------+
table2, table3, table4:
+----+---------+
| id | item_id |
+----+---------+
Here is my main query:
$sql = "SELECT t1.* FROM table1 AS t1, table2 AS t2 WHERE t2.id = '1' AND
t2.item_id = t1.item_id AND t1.type NOT IN ('type1', 'type2') LIMIT 5";
$a = mysql_query($sql);
while($b = mysql_fetch_assoc($a))
And now I want to integrate these 2 queries:
1st query:
AND IF t1.type = 'type3'
THEN
SELECT t3.item_id FROM table3 AS t3 WHERE t3.id = t1.product_id AND
t3.item_id NOT IN (SELECT t2.item_id FROM table2 AS t2 WHERE t2.id = '1')
ONLY if this statement t3.item_id NOT IN (...)
in the above query is true, then t1.id
from the main query should be displayed. However, if t3.item_id is IN (...)
then it should be excluded from the results.
2nd query:
AND IF t1.type = 'type4'
THEN
SELECT t4.item_id FROM table4 AS t4 WHERE t4.id = t1.product_id AND
t4.item_id NOT IN (SELECT t2.item_id FROM table2 AS t2 WHERE t2.id = '1')
Same as with the 1st query.
Does this all fit into one query? I'd need it in one query only, so I know which results to show on the next page, e.g. LIMIT 5,5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我理解是否正确,但我认为应该使用
(NOT) EXISTS
:I'm not sure I understand correctly but I think
(NOT) EXISTS
should be used:应用
流控制流结构
在 mysql 中或者:
apply the
flow control flow structure
in mysqlOr: