我有两个表,一个是城市,另一个是pois。我想从查询字符串中查找城市或pois。查询字符串可以是ABC咖啡店或旧金山。执行以下查询但返回空设置建议?
SELECT cities.city, cities.state, pois.name FROM cities, pois
WHERE cities.city OR pois.name LIKE '$queryString%';
表:城市
+---------+-------+---------------+
| city_id | state | city |
+---------+-------+---------------+
| 4 | CA | San Francisco |
+---------+-------+---------------+
表:pois
+--------+---------+-------------------------------+
| loc_id | city_id | name |
+--------+---------+-------------------------------+
| 2 | 4 | San Francisco Coffee Roasters |
+--------+---------+-------------------------------+
更新:根据@ Johan的回答,我创建了以下查询,返回下面的结果。返回的结果是正确的,但我还想返回一个单独的行,如示例A使用类似于示例A查询的结果。我认为工会是我需要的?
SELECT p.name, c.city, c.state
FROM cities c
INNER JOIN pois p ON ( p.city_id = c.city_id )
WHERE p.name LIKE '%San Francisco%'
+-------------------------------+---------------+-------+
| name | city | state |
+-------------------------------+---------------+-------+
| San Francisco Coffee Roasters | San Francisco | CA |
+-------------------------------+---------------+-------+
示例查询: !此查询返回错误#1222 - 使用的SELECT语句具有不同的列数
SELECT p.name, c.city, c.state
FROM cities c
INNER JOIN pois p ON ( p.city_id = c.city_id )
WHERE p.name LIKE '%San Francisco%'
UNION SELECT c.city, c.state
FROM cities c
WHERE c.city LIKE '%San Francisco%'
示例A结果:
+-------------------------------+---------------+-------+
| name | city | state |
+-------------------------------+---------------+-------+
| San Francisco Coffee Roasters | San Francisco | CA |
+-------------------------------+---------------+-------+
+---------------+-------+
| city | state |
+---------------+-------+
| San Francisco | CA |
+---------------+-------+
答案 0 :(得分:3)
事先发表一些意见:
•不要使用SQL '89样式的隐式连接语法,它是一种反模式
•使用显式SQL '92连接语法,请参阅下面的第一个示例
•如果$querystring
来自用户,则需要将其转义,请参阅第一个示例。
以下是示例代码:
$querystring = mysql_real_escape_string($querystring);
$query = "SELECT c.name, p.name FROM cities c
INNER JOIN pios p ON (p.city_id = c.id)
WHERE c.name LIKE '%$querystring%'
OR p.name LIKE '%$querystring%' ";
或许你想要一个UNION
$query = "SELECT c.name FROM cities c
WHERE c.name LIKE '%$querystring%'
UNION
SELECT p.name FROM pios p
WHERE p.name LIKE '%$querystring%' ";
根据要求编辑
如果使用union,则所有涉及的选择必须生成相同数量的列
如果您没有数据,只需替换null
。
SELECT p.name, c.city, c.state
FROM cities c
INNER JOIN pois p ON ( p.city_id = c.city_id )
WHERE p.name LIKE '%San Francisco%'
UNION
SELECT NULL as name, c.city, c.state
FROM cities c
WHERE c.city LIKE '%San Francisco%'
答案 1 :(得分:2)
使用连接执行此操作:
SELECT C.city, C.state, P.name
FROM cities C
LEFT JOIN pois P ON P.city_id=C.id
WHERE C.city LIKE '$queryString%'
OR P.name LIKE '$queryString%'
答案 2 :(得分:1)
SELECT DISTINCT p.name, c.city, c.state
FROM cities c
LEFT OUTER JOIN pois p ON p.city_id = c.city_id
WHERE cities.city LIKE '%$queryString%'
OR pois.name LIKE '%$queryString%';
或者,
SELECT p.name, c.city, c.state
FROM cities c
INNER JOIN pois p ON p.city_id = c.city_id
WHERE p.name LIKE '%$queryString%'
UNION SELECT NULL, c.city, c.state
FROM cities c
WHERE c.city LIKE '%$queryString%'
答案 3 :(得分:0)
SELECT cities.city, cities.state, pois.name FROM cities, pois
WHERE cities.city LIKE '$queryString%' OR pois.name LIKE '$queryString%';