简体   繁体   中英

Display data from one table when a column value in first table matches column value in second table

I am having a really hard time with this one. I have two different tables namely "departure" and "editor data". I want to display data from departure when the data in "Name" column of departure matches the data in the "ven" column of editor_data.

i've tried the SQL query below, but it doesn't work properly.

 <?php

 SELECT Time, Location, Name FROM departure JOIN editor_data 
 ON departure.Name = editor_data.ven 

  ?>

eg

departure table can have either mike or rite in the "Name" field. So, if the "editor_data" has the value "mike" in the "ven" column then it should only display data related to "mike" and exclude rite's data.

Select only those records of departure table where name exists in editor_data.ven column

SELECT Time, Location, Name FROM departure where name in (SELECT ven from editor_data); 

Your issue arises because you try to join two tables based on the column name values which is not a PRIMARY KEY. Hence, the join condition fails and it results in a cross join, which returns the cartesian product combination of all records present. Hence, the issue can be avoided by calling a subquery on a selected (limited) list of values.

这就是我的工作方式

"SELECT Time, Location, Name FROM departure where Name LIKE (SELECT ven from editor_data where tour_id = '$id');"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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