根据另一个表中的存在从表中进行选择

时间:2018-08-08 05:02:10

标签: php mysql sql

我想从表articles中进行选择,取决于code是否在名为inStock的表中。因此,例如,如果我在表code的{​​{1}}列中有一个代码,对于每个项目,我都需要选择* FROM inStock。有帮助吗?

这是表articles

enter image description here

这是表inStock

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试以下查询:使用内部联接,您可以获取此信息,因为代码需要同时显示在两个表中,希望您会获得所需的答案

select a.id,a.code,a.name,a.description,a.amount,a.color,b.price
from articles a inner join instock b
on a.code=b.code

答案 1 :(得分:1)

SELECT articles.*, inStock.Price
FROM articles INNER JOIN inStock ON articles.code = inStock.code

INNER JOIN将基于您要连接的列返回两个表中存在的记录

答案 2 :(得分:1)

请尝试以下查询:

SELECT `A`.*, `S`.`price` 
FROM inStock S 
JOIN articles A 
ON (`S`.`code` = `A`.`code`);

这里您有一个Demo