简体   繁体   中英

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. I need to extract the content from these both colums and want to get ID's from each record that is the same. The following query extracts what I already know/have, but I don't get the ID's from the WHERE IN table.

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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