Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何将这些查询整合到一个查询中?_Mysql_If Statement - Fatal编程技术网

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

Mysql 如何将这些查询整合到一个查询中?,mysql,if-statement,Mysql,If Statement,好吧,我要再试一次。。。这一次我肯定做对了。很抱歉搞混了 表1: 表2、表3、表4: 以下是我的主要问题: 现在我想集成这两个查询: 第一个问题: 如果t1.type='type3',那么 仅当此语句t3.item_id不在。。。在上面的查询中,如果为true,则应显示主查询中的t1.id。但是,如果t3.item_id在。。。那么它应该被排除在结果之外 第二个问题: 如果t1.type='type4',那么 与第一个查询相同 所有这些是否都适合一个查询?我只需要在一个查询中使用它,这样我就知道

好吧,我要再试一次。。。这一次我肯定做对了。很抱歉搞混了

表1:

表2、表3、表4:

以下是我的主要问题:

现在我想集成这两个查询:

第一个问题:

如果t1.type='type3',那么

仅当此语句t3.item_id不在。。。在上面的查询中,如果为true,则应显示主查询中的t1.id。但是,如果t3.item_id在。。。那么它应该被排除在结果之外

第二个问题:

如果t1.type='type4',那么

与第一个查询相同

所有这些是否都适合一个查询?我只需要在一个查询中使用它,这样我就知道下一页要显示哪些结果,例如,在mysql中应用LIMIT 5,5

或:


我不确定我是否理解正确,但我认为不应该使用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

好吧,我试试看!谢谢:不是很清楚,但我想你可以用EXISTS关键字。
+----+---------+
| 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))
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')
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')
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
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