选择不同表的多个值
我对 MySql 比较陌生。
我有 2 个具有以下结构的表格
products
{
pid-autoincrement,
pname
p_desc
}
services
{
sid-autoincrement
s_name
s_desc
}
我正在尝试选择名称为 '%
的产品或服务
我正在使用查询:
SELECT DISTINCT products.*,
services.*
FROM products, services
WHERE products.pname
LIKE '%mob%'
OR services.s_name
LIKE '%mob%'
但我得到了很多重复的结果。
尝试使用连接但无法得到解决方案。
有人可以帮我弄这个吗?
I'm relatively new to MySql.
I have 2 tabled with the following structure
products
{
pid-autoincrement,
pname
p_desc
}
services
{
sid-autoincrement
s_name
s_desc
}
Im trying to select the products or services which have the name '%<somekeyword>%'
I'm using the query:
SELECT DISTINCT products.*,
services.*
FROM products, services
WHERE products.pname
LIKE '%mob%'
OR services.s_name
LIKE '%mob%'
But I'm getting a lot of repeated results.
Tried using joins but couldn't get a solution.
Can someone help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想使用 UNION,如下所示:
You want to use UNION, like this:
您必须使用 UNION 运算符,
因此您只能选择相同的列(因为只有一个结果)
或者您选择 2 次 - 2 个结果
you have to use the UNION operator
therefore you can only select the same columns (as there is only one result)
or you select 2 times - 2 results
因为这些表不相关,所以您必须使用 UNION 运算符(如 karlis 所说),而不是 JOIN。 但是,由于您显然希望一起处理产品和服务(至少在某些时候),您最好将它们放入一个表中并添加一列来区分它们,如下所示:
Because the tables aren't related you'll have to use the UNION operator (as karlis says), rather than JOIN. But as you apparently want to handle products and services together (at least some of the time) you're probably better off putting them into one table and adding a column to differentiate between them, like so: