简体   繁体   English

MySQL:如何在1个条件下从2个不同的表中获取结果

[英]MySQL: How to get result from 2 different tables with 1 WHERE IN CLAUSE

I have 2 tables with different content and there is no way to join tables because there is no common identifier, but every table as 1 column with same content. 我有2个具有不同内容的表,由于没有公共标识符,所以无法联接表,但是每个表都是1列,具有相同的内容。 I need to extract the content from these both colums and want to get ID's from each record that is the same. 我需要从这两个列中提取内容,并希望从相同的每条记录中获取ID。 The following query extracts what I already know/have, but I don't get the ID's from the WHERE IN table. 以下查询提取了我已经知道/已经拥有的内容,但是我没有从WHERE IN表中获取ID。

Query 询问

SELECT pd.products_id, pd.products_short_description 
FROM products_description pd
WHERE pd.products_short_description IN (SELECT m.manufacturers_ean FROM manufacturers m)

Result 结果

ID | products_short_description
-------------------------------
2  | BMW

This is what I need: 这就是我需要的:

ID | products_short_description | ID | manufacturers_ean
--------------------------------------------------------
2  | BMW                        | 285| BMW
SELECT pd.products_id, pd.products_short_description, m.manufacturers_id, m.manufacturers_ean
FROM products_description pd INNER JOIN manufacturers m ON pd.products_short_description = m.manufacturers_ean;

in this case What you do with where could be done whit inner join and so you could select the two column from manufacturers_ean too 在这种情况下,您可以在内部连接处做什么,因此您也可以从Manufacturers_ean中选择两列

SELECT pd.products_id, pd.products_short_description, m.ID m.manufacturers_ean
FROM products_description pd
INNER JOIN manufacturers m on m.manufacturers_ean  = pd.products_short_description 

Query Will be : 查询将是:

 SELECT pd.products_id, pd.products_short_description, m.ID, m.manufacturers_ean
    FROM products_description pd,manufacturers m 
    WHERE m.manufacturers_ean = pd.products_short_description

Answer to all: Perfect! 所有人的答案:完美! It works! 有用! Where an I mark it as solved? 我在哪里标记为已解决?

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

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