如何将这些查询整合到一个查询中?

发布于 2024-11-26 17:10:13 字数 1445 浏览 1 评论 0原文

好吧,我再试一次……这次我绝对做对了。很抱歉造成混乱。 :(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2024-12-03 17:10:13

我不确定我理解是否正确,但我认为应该使用 (NOT) EXISTS

SELECT t1.* 
FROM table1 AS t1
  JOIN table2 AS t2 
    ON t2.item_id = t1.item_id
WHERE t2.id = '1' 
  AND t1.type NOT IN ('type1', 'type2')
  AND NOT ( t1.type = 'type3' 
            AND EXISTS 
                ( 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' )
                )
           ) 
  AND NOT ( t1.type = 'type4' 
            AND EXISTS 
                ( 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' )
                )
           ) 
ORDER BY WhatYouWant
LIMIT x,y

I'm not sure I understand correctly but I think (NOT) EXISTS should be used:

SELECT t1.* 
FROM table1 AS t1
  JOIN table2 AS t2 
    ON t2.item_id = t1.item_id
WHERE t2.id = '1' 
  AND t1.type NOT IN ('type1', 'type2')
  AND NOT ( t1.type = 'type3' 
            AND EXISTS 
                ( 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' )
                )
           ) 
  AND NOT ( t1.type = 'type4' 
            AND EXISTS 
                ( 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' )
                )
           ) 
ORDER BY WhatYouWant
LIMIT x,y
荒芜了季节 2024-12-03 17:10:13

应用流控制流结构 在 mysql 中

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

或者:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE

apply the flow control flow structure in mysql

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

Or:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文