简体   繁体   English

查询合并有相同列名的两个表

[英]Query on two tables merged with same column names

I have these tables in my MySQL database: 我的MySQL数据库中有这些表:

BUYERS 买家

ID|SELLER 
----------------
0 |Paul
1 |Jean
2 |David
3 |Jack
4 |John
5 |Fred
6 |Peter

PARIS 巴黎

ID|CAR
---------
0 |Toyota
1 |BMW
2 |Honda

LONDON 伦敦

ID|CAR
---------
3 |Ford
4 |BMW
5 |Honda
6 |Honda

I use the followinq query : 我使用followinq查询:

SELECT b.id, b.seller, p.car 
FROM buyers b
JOIN paris p
ON b.id = p.id
UNION ALL
SELECT b.id, b.seller, l.car 
FROM buyers b
JOIN london l
ON g.id = l.id;

To get the following result : 得到以下结果:

ID|SELLER |CAR 
----------------
0 |Paul   |Toyota
1 |Jean   |BMW
2 |David  |Honda
3 |Jack   |Ford
4 |John   |BMW
5 |Fred   |Honda
6 |Peter  |Honda

I wanted to retrieve rows with "Honda" as "CAR" and I Tried to append the query with "Where car = 'Honda'" but without success.. 我想用“ Honda”作为“ CAR”来检索行,并且试图在查询中附加“ Where car ='Honda'”,但没有成功。

Thanks for any help 谢谢你的帮助

Just appending WHERE car = 'Honda' is ambiguous. 仅添加WHERE car = 'Honda'是不明确的。 Which car column should be checked? 应该检查哪辆车列?

The easiest way to achieve this is to wrap your existing query within another select statement so that the query is applied on the resulting table, ie 实现此目的的最简单方法是将现有查询包装在另一个select语句中,以便将该查询应用于结果表,即

SELECT * FROM
(
    SELECT b.id, b.seller, p.car 
    FROM buyers b
    JOIN paris p
    ON b.id = p.id
    UNION ALL
    SELECT b.id, b.seller, l.car 
    FROM buyers b
    JOIN london l
    ON g.id = l.id;
)
WHERE car = 'Honda'

Adding 添加

WHERE car = 'Honda';

to your query only refers to the second query, ie the one after UNION ALL . 您的查询仅引用第二个查询,即UNION ALL之后的查询。

So either: 所以:

SELECT b.id, b.seller, p.car FROM buyers b JOIN paris p ON b.id = p.id
  WHERE p.car = 'Honda'
UNION ALL
SELECT b.id, b.seller, l.car FROM buyers b JOIN london l ON b.id = l.id
  WHERE p.car = 'Honda'
;

or 要么

SELECT id, seller, car
FROM
(
  SELECT b.id, b.seller, p.car FROM buyers b JOIN paris p ON b.id = p.id
  UNION ALL
  SELECT b.id, b.seller, l.car FROM buyers b JOIN london l ON b.id = l.id
) data
WHERE car = 'Honda';

Can you please try below query: 您能不能尝试以下查询:

select * from (
SELECT b.id, b.seller, p.car 
FROM buyers b
JOIN paris p
ON b.id = p.id
UNION ALL
SELECT b.id, b.seller, l.car 
FROM buyers b
JOIN london l
ON g.id = l.id;
)
where car = 'HONDA'

This is not an answer, but mere advice. 这不是答案,而仅仅是建议。

Just in case this is your real database (I'm pretty sure it isn't), here is some advice: 以防万一这是您的真实数据库(我敢肯定不是),这是一些建议:

  • The table names tell us what entities you are dealing with. 表格名称告诉我们您要处理的实体。 In your database this is buyers, parises, and londons. 在您的数据库中,这是买家,帕里斯和伦敦。 You probably see your mistake ;-) 您可能会看到您的错误;-)
  • Then looking into the buyers table we see the main column is called seller. 然后查看买家表,我们看到主要列称为卖家。 What the heck? 有没有搞错? Is it buyer or seller? 是买家还是卖家?
  • Then a column called ID should be the table's ID and uniquely identify a record in the table. 然后,称为ID的列应为表的ID,并唯一标识表中的记录。 You, however, are using the buyers IDs in other tables and still call them ID. 但是,您正在其他表中使用买家ID,但仍将其称为ID。
  • In your example each buyer has only one other record either in London or in Paris. 在您的示例中,每个买家在伦敦或巴黎只有一个​​其他记录。 If this is the case, then you can simply make the car a column in the buyers table instead. 如果是这种情况,那么您可以简单地将汽车设为Buyers表中的一列。 If you do need an n:m relation, then call the IDs by what they are, ie buyer_id or the like. 如果确实需要一个n:m关系, buyer_id ID的名称(即buyer_id等)来调用ID。
  • In any case there should be a car table containing one record per car, in order to avoid misspellings like 'Homda' in some records. 在任何情况下,都应该有一个载有每辆车一个记录的车表,以避免某些记录中出现诸如“ Homda”之类的拼写错误。
  • You are showing car brands, but call the table cars . 您正在显示汽车品牌,但将其称为台式cars If it's really about brands only, a better name would hence be brands or car_brands or the like. 如果它的的确确是品牌而已,一个更好的名字将因此成为brandscar_brands等。

Here is an example on how to design the database: 这是有关如何设计数据库的示例:

Cars 汽车

id_car | name
-------+-------
1      | BMW
2      | Ford
3      | Honda
4      | Toyota

Sellers 卖家

id_seller | name
----------+------
0         | Paul
1         | Jean
2         | David
3         | Jack
4         | John
5         | Fred
6         | Peter

Sellers_Cars Sellers_Cars

id_seller | id_car
----------+-------
0         | 1
0         | 2
0         | 4
1         | 1
2         | 3
3         | 2
4         | 1
5         | 3
6         | 3
6         | 4

A possible query: 可能的查询:

select name as honda_seller
from sellers
where id_seller in
(
  select id_seller
  from sellers_cars
  where car_id = (select car_id from cars where name = 'Honda')
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM