简体   繁体   English

使用内部联接SQL遇到麻烦

[英]Having trouble with Inner Join SQL

I've got two tables: 我有两个桌子:

Cyclist = Dob , name , ISO_id ISO_id = DobnameISO_id

Country = country_name , gdp , population , ISO_id . 国家= country_namegdppopulationISO_id

I need to query to get the Cyclist.name, Country.country_name, Country.gdp, Country.population. 我需要查询以获取Cyclist.name,Country.country_name,Country.gdp,Country.population。

Using two dates which are entered by the user. 使用用户输入的两个日期。 These two dates are used to search all cyclists who were born between the dates. 这两个日期用于搜索所有在两个日期之间出生的自行车手。

This is what i've tried so far: 到目前为止,这是我尝试过的:

SELECT Cyclist.name, Country.country_name, Country.gdp, Country.population
FROM Cyclist
INNER JOIN Country
ON Cyclist.ISO_id = Country.ISO_id
WHERE Cyclist.dob between '$date1' AND '$date2'

however it's not working and I dont know why. 但是它不起作用,我不知道为什么。

Can anyone offer any insight? 谁能提供任何见解?

You can use 您可以使用

SELECT Cyclist.name, Country.country_name, Country.gdp, Country.population
FROM Cyclist
LEFT JOIN Country
ON Cyclist.ISO_id = Country.ISO_id
Cyclist.dob>='$date1' and Cyclist.dob<='$date2'

Use left join for select all right recods from Cyclist and extend response by data from Country: 使用左联接从自行车手中选择所有右脚蹬,并根据国家/地区的数据扩展响应:

SELECT Cyclist.name, Country.country_name, Country.gdp, Country.population
FROM Cyclist
LEFT JOIN Country
ON Cyclist.ISO_id = Country.ISO_id
WHERE `Cyclist.dob` between '$date1' AND '$date2'

You have enclosed whole Cyclist.dob in WHERE clause, You need seperate enclose both Cyclist and dob to work like. 您已将整个Cyclist.dob包含在WHERE子句中,您需要将Cyclist和dob分别封闭以使其工作。

SELECT Cyclist.name, Country.country_name, Country.gdp, Country.population
FROM Cyclist
INNER JOIN Country
ON Cyclist.ISO_id = Country.ISO_id
WHERE `Cyclist`.`dob` between '$date1' AND '$date2'

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

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