如何显示两个表中的数据

时间:2014-09-10 19:13:10

标签: mysql

mysql> desc customer_delivery_loc;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| cust_loc_id   | int(11)      | NO   | PRI | NULL    | auto_increment |
| customer_id   | int(11)      | YES  | MUL | NULL    |                |
| locality      | varchar(100) | YES  |     | NULL    |                |
| area          | varchar(100) | YES  |     | NULL    |                |
| address       | varchar(100) | YES  |     | NULL    |                |
| city          | varchar(50)  | YES  |     | NULL    |                |
| state         | varchar(50)  | YES  |     | NULL    |                |
| phone_number  | bigint(20)   | YES  |     | NULL    |                |
| +---------------+--------------+------+-----+---------+----------------+



mysql> desc vendor_home_delivery;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| vendor_id | varchar(50)  | YES  | MUL | NULL    |       |
| locality  | varchar(100) | YES  |     | NULL    |       |
| area      | varchar(100) | YES  |     | NULL    |       |
| address   | varchar(100) | YES  |     | NULL    |       |
| city      | varchar(50)  | YES  |     | NULL    |       |
| state     | varchar(50)  | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

我有两个表,如上所示

area 对两个表都很常见。

将从用户界面区域 phone_number 传递。

如何编写查询以根据 phone_number 区域显示两个表中的数据?

我试过这种方式,但我不确定并给出错误

select c.phone_number 
from customer_delivery_loc c 
where c.area in (select locality , address , area from vendor_home_delivery ) 
where c.phone_number = '9848032919';

- MySQL数据库错误:您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便在'其中使用正确的语法,其中c.phone_number =' 9848032919''在第1行

2 个答案:

答案 0 :(得分:1)

你需要

and c.phone_number

取代

 where c.phone_number
在您的查询中

答案 1 :(得分:1)

由于 @Ollie Jones 已经指出另一个错误操作数应包含1个列错误,因此您可以使用join

select c.phone_number 
from customer_delivery_loc c 
join vendor_home_delivery  v 
 on(c.area = v.locality or c.area=v.address or c.area=v.area)
where c.phone_number = '9848032919';

此错误的原因是您使用in()

将单列区域与3列进行比较