Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL:如何获取表的列表对_Mysql_Sql_Select - Fatal编程技术网

Mysql SQL:如何获取表的列表对

Mysql SQL:如何获取表的列表对,mysql,sql,select,Mysql,Sql,Select,要求:列出经常光顾同一酒吧的顾客对(即,列出可能在咖啡馆遇到的所有顾客对) 吧台: ------------------------- id| name ------------------------- 1 | Vivamus nibh 2 | odio tristique 3 | vulputate ullamcorper 4 | Cras lorem 5 | libero est,

要求:列出经常光顾同一酒吧的顾客对(即,列出可能在咖啡馆遇到的所有顾客对)

吧台:

-------------------------
id| name                 
-------------------------
1 | Vivamus nibh         
2 | odio tristique       
3 | vulputate ullamcorper
4 | Cras lorem           
5 | libero est,          
客户表:

-----------------------
id| name              
-----------------------
1 | Warren    
2 | Olympia            
3 | Logan
4 | Summer
5 | Kamal
6 | Fernandez
常客表:

-----------------
cust_id | bar_id
-----------------
1       | 1
2       | 1
3       | 2
4       | 2
5       | 3
6       | 4
预期产出:

---------------------------------------
customer1 | customer2 | barname
---------------------------------------
Warren    |  Olympia  | Vivamus nibh
Logan     |  Summer   | odio tristique
这是我的尝试,但没有成功:

select c1.name, c2.name, b1.name, b2.name
from frequents f1, frequents f2
join bar b1 on f1.bar_id = b1.id
join bar b2 on f2.bar_id = b2.id
join customer c1 on f1.cust_id = c1.id
join customer c2 on f2.cust_id = c2.id
where f1.bar_id = f2.bar_id;

您可以将bar表与frequency表联接两次,然后继续联接以获取客户名称。为了防止重复,您可以任意决定一个
cust\u id
应小于另一个:

SELECT b.name, c1.name, c2.name
FROM   bar b
JOIN   frequents f1 ON f1.bar_id = b.id
JOIN   frequents f2 ON f2.bar_id = b.id AND f1.cust_id < f2.cust_id
JOIN   customer  c1 ON c1.id = f1.cust_id
JOIN   customer  c2 ON c2.id = f2.cust_id
选择b.name、c1.name、c2.name
从酒吧b
在f1.bar\u id=b.id上连接频繁的f1
在f2.bar\u id=b.id和f1.cust\u id

您可以将bar表与frequency表联接两次,然后继续联接以获取客户名称。为了防止重复,您可以任意决定一个
cust\u id
应小于另一个:

SELECT b.name, c1.name, c2.name
FROM   bar b
JOIN   frequents f1 ON f1.bar_id = b.id
JOIN   frequents f2 ON f2.bar_id = b.id AND f1.cust_id < f2.cust_id
JOIN   customer  c1 ON c1.id = f1.cust_id
JOIN   customer  c2 ON c2.id = f2.cust_id
选择b.name、c1.name、c2.name
从酒吧b
在f1.bar\u id=b.id上连接频繁的f1
在f2.bar\u id=b.id和f1.cust\u id

我正在使用子查询来加入具有相同酒吧但不同客户的常客,并通过使用>对客户id进行“排序”,以避免重复

SELECT c1.name customer1, f2.name customer2, b.name barname
FROM frequents f
JOIN customer c1 ON c1.id = f.cust_id
JOIN bar b ON f.bar_id = b.id
JOIN (SELECT cust_id, bar_id, name
      FROM frequents 
      JOIN customer ON id = cust_id) AS f2 ON f2.bar_id = f.bar_id AND f2.cust_id > f.cust_id 

DB Fiddle

我正在使用子查询来加入同一个酒吧但不同客户的常客,并使用>对客户id进行“排序”,以避免重复

SELECT c1.name customer1, f2.name customer2, b.name barname
FROM frequents f
JOIN customer c1 ON c1.id = f.cust_id
JOIN bar b ON f.bar_id = b.id
JOIN (SELECT cust_id, bar_id, name
      FROM frequents 
      JOIN customer ON id = cust_id) AS f2 ON f2.bar_id = f.bar_id AND f2.cust_id > f.cust_id 

DB Fiddle

要获得每个条的所有对,自加入Frequents表可以获得这些

然后,您只需在ID上连接其他表即可获得名称。


SqlFiddle test要获得每个条的所有对,自加入Frequents表可以得到

然后,您只需在ID上连接其他表即可获得名称。


SqlFiddle测试

看,这里不清楚什么是对,就像前面的评论指出的那样,我们需要样本数据和预期结果result@JoakimDanielson谢谢你提醒我。我已经编辑了。@PhuongThuan这两个要求看起来很相似。如果它们不同,则需要使用两种不同的预期输出突出显示差异。另外,您的示例数据还不够,因为它应该包含一些可能不成对的客户。目前,所有客户和酒吧都在制作一对可能的预期输出。@MadhurBhaiya我已经编辑过了。谢谢,这里不清楚什么是配对,就像前面的评论指出的那样,我们需要样本数据和预期的结果result@JoakimDanielson谢谢你提醒我。我已经编辑了。@PhuongThuan这两个要求看起来很相似。如果它们不同,则需要使用两种不同的预期输出突出显示差异。另外,您的示例数据还不够,因为它应该包含一些可能不成对的客户。目前,所有客户和酒吧都在制作一对可能的预期输出。@MadhurBhaiya我已经编辑过了。谢谢