简体   繁体   English

如何选择在另一个表中具有全部或没有相应值的行?

[英]How to select rows, which have all or none corresponding values in another table?

I'm not sure I phrased the question correctly, so feel free to correct me. 我不确定我是否正确地表达了这个问题,所以请随时纠正我。 Here are the tables with their data: 以下是包含其数据的表格:

product        category                 category_product
-------        --------                 ----------------
id_product     id_category  active      id_category  id_product
1              1            1           1            1
2              2            1           2            1
3              3            0           1            2
               4            0           2            2
                                        3            2
                                        3            3
                                        4            3

I need to select only those products, which have all categories as inactive. 我只需要选择那些将所有类别都设为非活动状态的产品。 For example: 例如:

  • Product 1 is good, since it belongs to active categories ( 1 , 2 ). 产品1是好的,因为它属于活性类别( 12 )。
  • Product 2 is good, since it has at least one active category ( 1 , 2 ; 3 - inactive) 产品2是良好的,因为它具有至少一个活性类别( 12 ; 3 -不活动)
  • Product 3 must be selected, since all its categories are inactive ( 3 , 4 ). 产物3必须选择,因为它的所有类别是不活动的( 34 )。

I have the following query, which is obviously incorrect, since it selects both products: 2 and 3 : 我有以下查询,这显然是不正确的,因为它选择了两个产品: 23

SELECT p.id_product
FROM product p
JOIN category_product cp
  ON p.id_product = cp.id_product
JOIN category c
  ON c.id_category = cp.id_category
WHERE
  c.active = 0;

Here is the SQL Fiddle: http://sqlfiddle.com/#!2/909dd/2/0 这是SQL小提琴: http ://sqlfiddle.com/#!2/909dd/2/0

How can I solve this? 我怎么解决这个问题?

This way you can select product without active category. 这样您就可以选择没有活动类别的产品。

SELECT p.id_product
FROM product p
WHERE NOT EXISTS     
    (SELECT * FROM 
     category_product cp  
     INNER JOIN category c ON c.id_category = cp.id_category
     WHERE p.id_product = cp.id_product AND c.active = 1);

SQL Fiddle SQL小提琴

This is what i get while trying...apologies if anything is wrong 这就是我在尝试时得到的......如果出现任何问题,请道歉

set @count:=0;
select a.id_product,a.times from 
    (SELECT    count(p.id_product)times, p.id_product, c.active, 
               if(c.active!=0, @count:=@count+1, @count:=0) x
    From category_product cp
         join  product p
               on (p.id_product = cp.id_product)
        join   category c
               on(c.id_category = cp.id_category )
    group by id_product )a 
where a.x=0;

Consider the following: 考虑以下:

SELECT p.* 
     , COUNT(*)
     , SUM(c.active = 1) active
     , SUM(c.active = 0) inactive
  FROM product p
  JOIN category_product cp 
    ON cp.id_product = p.id_product
  JOIN category c
    ON c.id_category = cp.id_category
 GROUP
    BY p.id_product;

+------------+----------+--------+----------+
| id_product | COUNT(*) | active | inactive |
+------------+----------+--------+----------+
|          1 |        2 |      2 |        0 |
|          2 |        3 |      2 |        1 |
|          3 |        2 |      0 |        2 |
+------------+----------+--------+----------+

http://sqlfiddle.com/#!2/909dd/55 http://sqlfiddle.com/#!2/909dd/55

The last part of this problem has been left as an exercise for the reader 这个问题的最后一部分留给读者练习

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL:Select 在第二个表中没有相应用户 ID 的行 - SQL: Select Rows which does not have a corresponding userID in second table 如何从B表的所有相应行中仅选择A的值都为X的条目? - How to select only entries from A which have only values=X in all respective rows in B table? 如何选择具有相同值的所有行 - How to select all rows which have identical values 如何在MySQL的另一个表中仅选择没有相应外键的行? - How do I select only rows that don't have a corresponding foreign key in another table in MySQL? 如何 select 一列具有其他列的最大值,以及另一个表中列的所有相应行? - How to select a column with max value of other column, and all corresponding rows of column in another table? MYSQL-选择表A中的值,其中表B中的所有相应值都具有特定值 - MYSQL - Select values in table A where all the corresponding values in table B have a specific values 如何选择表a中具有表b中给出的n个特征的所有行 - How can I select all rows in a table a, which have n characteristics given in a table b 查询未返回表中另一个[关联]表中没有对应值的行 - Query not returning rows in a table that don't have corresponding values in another [associative] table 如何从表中选择在相关表中没有对应行的行? - How do I select rows from a table that do not have corresponding rows in a related table? 如何不选择另一个表中存在的值 - How not to select values which exist in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM