多表查询相同字段内容的问题.

prettywolf 2006-02-24 09:51:23
如表:client结构为
CREATE TABLE `client` (
`name` varchar(16) NOT NULL default '',
`tel` varchar(24) NOT NULL default '',
`mobile` varchar(11) NOT NULL default '',
`email` varchar(24) NOT NULL default '',
` PRIMARY KEY (`name`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
worker的结构也差不多,只是多了一两个字段.
我想在一条语句内获得两个表中名字相同字段内容(name,mobile)
如:select name,mobile from client,worker
这条语句是出错的,大家应该明白我的意思了吧.
再讲详细点:
client:
name tel mobile email
prettywolf 8213800 13977455800 hello@163.com
gbpc 8213800 13977458888 gb@163.com

worker:
name tel mobile email
abc1234 8213788 13977488888 hello@163.com
zdbc 8213666 13977499999 gb@163.com

要求查询后返回结果为:

name mobile
prettywolf 13977455800
gbpc 13977458888
abc1234 13977488888
zdbc 13977499999

我也试过select * from client,worker
这样子在2个表各有2个记录时只能查到2条记录,因为2条粘到一起了.
大家帮帮忙.
...全文
147 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxzm1001 2006-02-26
  • 打赏
  • 举报
回复
select name,mobile from client
union all
select name,mobile from worker
ms44 2006-02-26
  • 打赏
  • 举报
回复
select name,mobile from client
union
select name,mobile from worker
这个应该没有错。你试一下,不要有空行。
prettywolf 2006-02-26
  • 打赏
  • 举报
回复
#1064 - You have an error in your SQL syntax near 'UNION
SELECT name, mobile
FROM worker LIMIT 0, 30' at line 3
XqYuan 2006-02-25
  • 打赏
  • 举报
回复
select name,mobile from client
union
select name,mobile from worker

56,682

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧