简体   繁体   中英

Select specific data from main table on joining in MySQL

在此处输入图片说明 I have Two tables.Table one is the master table containing name of some products, table two save the product assigned to specific users.Then i need to get the product list from the master table(table 1) which is not added under the user in table 2. above image shows the table structure.

So when I chose user 11 its should not how the product_name C. How can I write the MySQL query for this.

You may try this query

SELECT table1.product_name FROM `table1`
INNER JOIN table2 on table2.p_id = table1.p_id
WHERE table2.user_id = 11

I guees below query should work.

SELECT p1.p_id,p1.product_name FROM table_1 AS p1
INNER JOIN table_2 AS p2
ON p2.p_id = p1.p_id
WHERE p2.user_id = '11'

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