如果表2中存在所有值,则在表1中保留行

时间:2019-05-25 17:48:42

标签: mysql sql

在构造此sql查询时遇到了一些麻烦。

我在加拿大和美国的城市有一张桌子,像这样:

 (?mis)                        # Modifiers: multi-line, case-insensitive,  dot-all
 \A                            # Beginning of string
 (?:                           # Cluster group
      .*?                           # minimal, optional any characters
      ^                             # BOL
      Conversation [ ]+          
      with [ ]+ 
 ){3}                          # End cluster, do 3 times
 ( .*? )                       # (1), 3rd Name to capture
 (?= [ ]* \(https:// )         # Assert, https ahead of here

我还有一张只包含加拿大城市的桌子

+-------------+------------------+
|start        |finish            |
+-------------+------------------+
|      Calgary|         Vancouver|
|     Edmonton|           Toronto|
|     Montreal|          New York|
|      Chicago|          St Louis|
|        Miami|          Winnipeg|
+-------------+------------------+

我想过滤第一个表,以仅保留+-------------+ |cities | +-------------+ | Calgary| | Edmonton| | Montreal| | Vancouver| | Toronto| | Winnipeg| +-------------+ 表中同时存在startfinish的行,就像这样:

样品输出:

cities

我最初的想法是:

+-------------+------------------+
|start        |finish            |
+-------------+------------------+
|      Calgary|         Vancouver|
|     Edmonton|           Toronto|
+-------------+------------------+

但是我经过一番尝试和错误后才意识到,只有在开始和结束相同的情况下才会返回值。

SELECT *
FROM start_finish s, cities c
WHERE c.cities = s.start and c.cities = s.finish

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

内部加入两次。一次用于finish,一次用于SELECT sf1.start, sf1.finish FROM start_finish sf1 INNER JOIN cities c1 ON c1.cities = sf1.start INNER JOIN cities c2 ON c2.cities = sf1.finish;

function Beheer_notitiethemas()
{var i; var text;  
var left; var top; var width; var height; var jaar;
var mybox; var but1; var but2; var but3; var but4; 

whatdidisee = whatdoisee; 
whatdoisee = 0;

left = 80;  
top = 30;  
width = 850;  
height =600;

DOC = window.rechts.document;

  this box just waits to be displayd when the text in Buildtext() is    completed so both boxes apear at the same time 
 CreateBoxId(DOC, "pleasewaitbox", 10, 10, 200, 200, "#ffffcc", "please wait... ", 1, 1);


text = Buildtext();

mybox = ExistAnyBox();   
if(mybox == null)
{
}

db<>fiddle

答案 1 :(得分:1)

您可以使用joinexists

select sf.*
from start_finish sf
where exists (select 1 from cities c where c.city = sf.start
             ) and
      exists (select 1 from cities c where c.city = sf.finish
             ) ;