mysql-如何在同一表中选择行而不是2列而不是所有其他2列

时间:2019-07-29 11:49:59

标签: mysql

我需要从表中获取所有行中的所有行,其中所有行中的question_id <> targetquestion_type <> target_type

例如

+----+-------------+---------------+-------------+--------+-------+
| id | question_id | question_type | target_type | target | start |
+----+-------------+---------------+-------------+--------+-------+
|  1 |           3 |             0 |           2 | 1      |     1 |
|  3 |           1 |             1 |           2 | 3      |     0 |
|  5 |           3 |             0 |           1 | 3      |     0 |
+----+-------------+---------------+-------------+--------+-------+

我的结果应该是ID为3的第二行

+----+-------------+---------------+-------------+--------+-------+
| id | question_id | question_type | target_type | target | start |
+----+-------------+---------------+-------------+--------+-------+
|  3 |           1 |             1 |           2 | 3      |     0 |

我需要在第一行中同时使用question_idquestion_type并将它们与所有行中的targettarget_type进行比较 当question_idquestion_type不等于targettarget_type where start = 1question_idquestion_type when start = 1不出现在结果中时,获得行< / p>

因为我的第1行有start = 1,并且它的question_id = 3question_type = 0是,所以我想排除任何带有(question_id = 3question_type = 0)的行根据我的结果

第2行:question_id = 1question_type = 1在所有行中都不等于targettarget_type

这是我尝试过的:

SELECT s.* 
  FROM assign s 
  JOIN assign l1 
    ON s.question_id <> l1.target 
  JOIN assign l2 
    ON s.question_type <> l2.target_type 

SELECT *  
  FROM assign` 
 WHERE (question_id NOT IN (SELECT target from assign) ) 
   AND question_type <> target_type 
   AND scenario_id = 3 
   AND start = 0

预先感谢

0 个答案:

没有答案