简体   繁体   English

MySQL使用table1.id REGEXP table2.id为同一组选择不同的行

[英]MySQL select distinct rows for same group using table1.id REGEXP table2.id

Table name: groups 表名:组

id  name
1   ONE
2   TWO
3   THREE
4   FOUR
5   FIVE
6   SIX

Table name: titles 表格名称:标题

id  title           groups  isactive
1   First Title     1,2,4   yes
2   Second Title    2,5,7   yes
3   Third Title     3,1,2   yes
4   Fourth title    2,4,5   yes

Link column: id 链接列:ID

QUERY: 查询:

SELECT 
 t.*, g.name
FROM 
 `titles` AS t, groups AS g 
WHERE 
  t.groups REGEXP CONCAT('^', g.id, '')
ORDER by title ASC, name ASC

Results: 结果:

id  title           groups  isactive    name
1   First Title     1,2,4   yes         ONE
4   Fourth title    2,4,5   yes         TWO
2   Second Title    2,5,7   yes         TWO
3   Third Title     3,1,2   yes         THREE

Now, the problem is I want to select only one title for each group, however, there might be duplicate group names like (id = 4 and 2) both assigned to group number TWO. 现在,问题是我只希望为每个组选择一个标题,但是,可能会有重复的组名,如(id = 4和2)都分配给组号TWO。

HOW TO ONLY SHOW THE ONE WITH HIGH ID? 仅显示高ID的人? Then the result should be like this: (excluding id=2) 然后结果应该是这样的:(不包括id = 2)

id  title           groups  isactive    name
1   First Title     1,2,4   yes         ONE
4   Fourth title    2,4,5   yes         TWO
3   Third Title     3,1,2   yes         THREE

I tried using this query also: 我也尝试使用此查询:

SELECT 
 t.*, g.name
FROM 
 `titles` AS t, groups AS g 
WHERE 
  t.groups REGEXP CONCAT('^', g.id, '')
GROUP by g.name
ORDER by t.id DESC 

or 要么

ORDER by t.id ASC

But both showing: 但两者都显示:

id  title           groups  isactive   name
3   Third Title     3,1,2   yes        THREE
2   Second Title    2,5,7   yes        TWO
1   First Title     1,2,4   yes        ONE

PLEASE SEE DEMO - SQL FIDDLE 请查看演示-SQL FIDDLE

Try this 尝试这个

SELECT A.*, B.NAME FROM titles A
JOIN (
         SELECT g.name, max(t.id) id
         FROM titles AS t, groups AS g 
         WHERE t.groups REGEXP CONCAT('^', g.id, '')
         group by g.name
     ) B on A.id = b.id

SQL DEMO SQL演示

暂无
暂无

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

相关问题 MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id 通过在Table1.id上应用groupBy,但在Table2上没有软删除的行,使用Laravel查询生成器来计算总和? - Calculate sum using Laravel Query Builder by applying groupBy on Table1.id but without soft deleted rows on Table2? MySQL select 同组id的所有行 - MySQL select all rows with the same group of id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM