选择不同表的多个值

发布于 2024-07-13 12:16:21 字数 565 浏览 6 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别念他 2024-07-20 12:16:21

您想使用 UNION,如下所示:

SELECT DISTINCT products.pid-autoincrement AS id, 
                products.pname AS name, 
                products.p_desc AS desc 
           FROM products
          WHERE products.pname 
                LIKE '%mob%' 
UNION
SELECT DISTINCT services.sid-autoincrement AS id, 
                services.s_name AS name, 
                services.s_desc AS desc 
           FROM services
          WHERE services.s_name 
                LIKE '%mob%' 

You want to use UNION, like this:

SELECT DISTINCT products.pid-autoincrement AS id, 
                products.pname AS name, 
                products.p_desc AS desc 
           FROM products
          WHERE products.pname 
                LIKE '%mob%' 
UNION
SELECT DISTINCT services.sid-autoincrement AS id, 
                services.s_name AS name, 
                services.s_desc AS desc 
           FROM services
          WHERE services.s_name 
                LIKE '%mob%' 
带刺的爱情 2024-07-20 12:16:21

您必须使用 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

夜灵血窟げ 2024-07-20 12:16:21

因为这些表不相关,所以您必须使用 UNION 运算符(如 karlis 所说),而不是 JOIN。 但是,由于您显然希望一起处理产品和服务(至少在某些时候),您最好将它们放入一个表中并添加一列来区分它们,如下所示:

productsandservice [need better name]
{
id,
name,
desc,
isaservice
}

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:

productsandservice [need better name]
{
id,
name,
desc,
isaservice
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文