简体   繁体   English

MySQL:选择按列分组的所有条目,在另一列中使用相同的值

[英]MySQL: Select all entries grouped by a column with a common value in another column

I have the following table posts: 我有以下表格帖子:

id | user_id | post_title | is published
1    9332      some title   0
2    2133      some title   1
3    9332      some title   0
4    2133      some title   1
5    4555      some title   0
6    4555      some title   1
7    3221      some title   0
8    3221      some title   0

My goal: Looking for all entries which have the same user_id and of which there is no single entry with "is published" = 1. 我的目标:查找所有具有相同user_id且没有“发布” = 1的条目的条目。

So as an result I must get entries with the id 1, 3, 7 and 8 => these are entries of users who do not have any single published post so far. 因此,结果是我必须获得ID为1、3、7和8的条目=>这些是到目前为止还没有任何发布的帖子的用户的条目。

What would the query be like? 查询是什么样的?

EXTENDED: EXTENDED:

A fiddle can be found here: 在这里可以找到一个小提琴:

http://sqlfiddle.com/#!9/b58fb3/1/0 http://sqlfiddle.com/#!9/b58fb3/1/0

In addition, I created Table 2. My goal is that only Table 1 entries of users will be shown who do not have a linked entry in Table 2. So what I aim for is that after executing the query only Table 1 entries with the IDs 1 and 3 are left (only user 9332 entries). 另外,我创建了表2。我的目标是仅显示表1中没有链接条目的用户的表1条目。因此,我的目标是在执行查询后仅使用ID为ID的表1条目。剩下1和3(仅用户9332条目)。

Try This: 尝试这个:

SELECT 
    distinct user_id
FROM 
    table
WHERE user_id not in (SELECT DISTINCT user_id FROM table WHERE is_published = 1)

You can use not exists : 您可以使用not exists

SELECT t.* 
FROM t 
WHERE NOT EXISTS (SELECT 1 
                  FROM t t2 
                  WHERE t2.user_id = t.user_id AND
                        t2.is_published = 1
                 ); 

For your example, all users have at least two entries. 在您的示例中,所有用户至少有两个条目。 This query will return users with one unpublished entry. 该查询将向用户返回一个未发布的条目。 It is not clear if this is desirable or not. 尚不清楚这是否可取。 If not, it is pretty simple to modify the query. 如果不是,则修改查询非常简单。

Using Nested Query : 使用嵌套查询:

SELECT DISTINCT `id`, 
                `user_id`, 
                `post_title`, 
                `is published`
FROM table1
WHERE user_id NOT IN (SELECT DISTINCT user_id FROM table1 WHERE `is published` = 1)
AND user_id  NOT IN (SELECT DISTINCT T1.user_id
                     FROM Table1 T1
                     INNER JOIN Table2 T2
                     ON T1.id=T2.table1_id)

OR Using Joins : 或使用联接:

SELECT T1.*
FROM
Table1 T1
INNER JOIN
(
SELECT user_id,COUNT(*) AS CNT
FROM TABLE1 
GROUP BY user_id
) T2
ON T1.user_id=T2.user_id
INNER JOIN
(
SELECT user_id,COUNT(*) AS CNT FROM Table1
WHERE `is published` = 0
GROUP BY user_id
) T3
ON T2.user_id=T3.user_id
AND T2.CNT=T3.CNT
WHERE T1.user_id
NOT IN (
        SELECT T1.user_id
        FROM Table1 T1
        INNER JOIN Table2 T2
        ON T1.id=T2.table1_id
       )

Output 产量

id  user_id post_title  is published
1   9332    some title  0
3   9332    some title  0

Demo 演示

http://sqlfiddle.com/#!9/2d5cb8/12 http://sqlfiddle.com/#!9/2d5cb8/12

The simple way is to use SUBQUERY with GROUP BY and HAVING clause. 简单的方法是将SUBQUERYGROUP BYHAVING子句一起使用。 We can return duplicate rows but not having is_published = 1 then join with outer query to return the record of matching ids 我们可以返回重复的行,但没有is_published = 1然后与外部查询一起返回匹配ID的记录

SELECT t.id,
    t.user_id,
    t.post_title,
    t.is_published 
FROM temp t
INNER JOIN (SELECT user_id
            FROM temp
            GROUP BY user_id HAVING SUM(is_published) = 0) u ON u.user_id = t.user_id
ORDER BY t.id

OUTPUT: OUTPUT:

id  user_id post_title  is_published
1   9332    some title  0
3   9332    some title  0
7   3221    some title  0
8   3221    some title  0

Eg: 例如:

Note: Do not use spaces in table/column identifiers. 注意:请勿在表/列标识符中使用空格。 I have amended the column name accordingly. 我已经相应地修改了列名。

SELECT x.* 
  FROM table1 x 
  LEFT
  JOIN table1 y
    ON y.user_id = x.user_id
   AND y.is_published = 1
 WHERE x.is_published = 0
   AND y.id IS NULL;

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

相关问题 在mysql select语句中将公用列值作为所有结果的标题 - Make a common column value as the heading of all results in mysql select statement MySQL:选择具有最大列值并由另一列分组的对应日期时间的行 - MySQL: Select rows with max column value and corresponded datetime grouped by another column 如何使用mySQL选择具有一列最大值的结果,按另一列分组? - How do I use mySQL to select results which have a maximum value for one column, grouped by another column? 具有相同列值的MySQL SELECT数据库条目 - MySQL SELECT database entries with the same column value MySQL选择列,这是另一列中的值 - MySQL select column which is a value in another column mysql SELECT列值依赖于另一列 - mysql SELECT column value dependent on another column mysql 从另一列中选择列值 - mysql select column value from another column 仅当所有其他具有相同值的条目都存在并且匹配时,才如何选择列的值? (MYSQL) - How to select a column's value, only if all other entries with the same value exist and match? (MYSQL) MySQL选择行,其中一列具有公共值,而另一列具有最大值 - MySQL select row that has a common value on one column but max value of another mysql基于另一个选择的列的选择值 - mysql select value of column based on another select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM