简体   繁体   English

如何在SQL中比较来自同一表的行

[英]How to compare rows from same table in sql

My tables are: 我的表是:

frequents(bar,drinker);
likes(beer,drinker);
serves(bar,beer)

I want to "select pairs of drinkers who frequents exactly the same bars".I think I can write that query using only the frequents table (as it has both bar and drinker column) using self joints I tried to do but couldn't get it.I don't mind using other tables too to get the exact query.The query must select drinkers who goes to the same bars only.In other words they should have all the the bars in common.The query must be in generalized form it should not depend on data that's why I didn't put any data. 我想“选择几对经常光顾相同酒吧的饮酒者”。我想我可以使用我尝试做过但无法获取的自我关节,仅使用光顾者表格(因为它同时具有酒吧和饮酒者列)来编写该查询我也不介意使用其他表格来获取确切的查询。查询必须选择仅去相同酒吧的饮酒者,换句话说,他们应该具有所有相同的酒吧。查询必须采用广义形式它不应该依赖于数据,这就是为什么我没有放入任何数据。

DRINKER |     BAR  
____________________    
John    |     Hyatt       
Smith   | Blue     
William | Hilton   
John    | Geoffreys 
Smith   | Hyatt     
Joe     | Blue      
Mike    | Hilton    
William | Dublin        
Jeff    | Hilton        
Jake    | Hilton    

This is my frequents table I need to select only Joe and Smith and also Jake and Jeff because they visit exactly the same bars. 这是我的常客表,我只需要选择Joe和Smith以及Jake和Jeff,因为他们访问的酒吧完全相同。

THe easiest way in MySQL is to use group_concat() to put the values together and compare: MySQL中最简单的方法是使用group_concat()将值放在一起并进行比较:

select fd.bars, group_concat(fd.drinker) as drinkers
from (select f.drinker, group_concat(f.bar order by f.bar) as bars
      from frequents f
      group by f.drinker
     ) fd
group by bars
having count(*) > 1;

EDIT 编辑

You can also do this using join s, but to do it right, you need a full outer join -- which MySQL does not support. 您也可以使用join来执行此操作,但要正确执行此操作,您需要一个full outer join -MySQL不支持。

Another way is to count the number of bars that each goes to, do an inner join, and be sure that the counts match as well as the bars: 另一种方法是计算每个去往的小节的数量,进行内部联接,并确保计数和小节都匹配:

select f1.drinker, f2.drinker
from frequents f1 join
     frequents f2
     on f1.bar = f2.bar join
     (select f.drinker, count(*) as numbars
      from frequents f
      group by f.drinker
     ) fd1
     on f1.drinker = fd1.numbars join
     (select f.drinker, count(*) as numbars
      from frequents f
      group by f.drinker
     ) fd2
     on f2.drinker = f2.drinker
group by f1.drinker, f2.drinker
having count(*) = max(f1.numbars) and count(*) = max(f2.numbars);

This may help, however I'm using my knowledge from Microsoft SQL server. 这可能会有所帮助,但是我正在使用来自Microsoft SQL Server的知识。 The syntax may differ from MYSQL. 语法可能与MYSQL不同。 That being said you would need to JOIN the tables into themselves. 话虽这么说,您需要将表联接到自己。

SELECT F.Bars, F.Drinkers, FB.Drinker 
FROM frequents F 
JOIN frequents FB ON (FB.Drinkers = F.Bars)
WHERE FB.Drinkers = F.Drinkers

basically I join the same table to itself saying the FB.Drinkers contains the bars that drinkers go to and F.Drinkers is the list of people who drink. 基本上,我在同一张桌子上也说FB.Drinkers包含饮酒者去的酒吧,而F.Drinkers就是喝酒的人的名单。 I'm not 100% on the syntax but the idea is correct. 我不是100%的语法,但是这个想法是正确的。

I haven't done SQL in 4 months. 我已经四个月没有做SQL了。 if you don't use it, you loose it :) 如果您不使用它,则将其松开:)

fixnode's answer is pretty good, although it lacks 2 little details that I think are important, select only non repeated results and sort them in a useful way. fixnode的答案相当不错,尽管它缺少2个我认为很重要的小细节,仅选择非重复结果并以有用的方式对其进行排序。

SELECT DISTINCT tbl_1.drinker, tbl_1.bar
FROM `frequents` AS tbl_1
LEFT JOIN frequents AS tbl_2 ON tbl_1.bar = tbl_2.bar WHERE tbl_1.bar = tbl_2.bar
ORDER BY tbl_1.bar;

This is what I used for testing 这就是我用来测试的

CREATE TABLE IF NOT EXISTS `frequents` (
  `bar` varchar(100) NOT NULL,
  `drinker` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `frequents` (`bar`, `drinker`) VALUES
('bar_1', 'drinker_3'),
('bar_2', 'drinker_3'),
('bar_3', 'drinker_4'),
('bar_4', 'drinker_1'),
('bar_5', 'drinker_1'),
('bar_1', 'drinker_5'),
('bar_2', 'drinker_4'),
('bar_3', 'drinker_3'),
('bar_4', 'drinker_2'),
('bar_5', 'drinker_1');

Well you are not showing anyone desired output but if you add a table for the bar, then this will work for you 好吧,您没有显示任何所需的输出,但是如果您为条形图添加表格,那么它将为您工作

select f.bar, f.drinker from frequents f
inner join bar b on b.bar = f.bar

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM