我正在尝试在单个数据库中搜索多个表,但我没有运气。
我有两个表,城市和国家/地区,我想要一个搜索结果来自两个/
像这样 -
SELECT * FROM cities && countries WHERE name ='New York'
任何帮助都会很棒!
答案 0 :(得分:15)
这可以使用JOIN
或UNION
子句来完成。取决于您希望结果的样子。 (我在以下示例中对您的架构做了一些假设):
SELECT *
FROM cities
JOIN countries ON (cities.country_id = country.id)
WHERE cities.name = 'New York'
OR countries.name = 'New York'
ALL
,出于性能原因)SELECT cities.name, 'Is a city' AS type
FROM cities
WHERE cities.name = 'New York'
UNION ALL
SELECT countries.name, 'Is a country' AS type
FROM countries
WHERE countries.name = 'New York'