简体   繁体   English

从两个表中提取SQL数据

[英]Pull SQL data from two tables

I have a table called db.data and db.info . 我有一个名为db.datadb.info的表。 The db.data table has a name and dateadded column for every entry. db.data表的每个条目都有一个namedateadded列。 The info table has a name and a status column for every name (status is either y or n ). info表具有每个namestatus列(状态为yn )。 I would like to pull the data from the db.data table for all the names added to that db by date but only if the status (the one in db.status) is n. 我想从db.data表中提取所有按日期添加到该数据库的名称的数据,但前提是状态(db.status中的状态)为n。

I have been looking around and found JOIN , but that seems to only work if the data is the same in both tables. 我一直在寻找并找到JOIN ,但这似乎只有在两个表中的数据相同时才有效。 Here, the names are the same, but I want the data extracted from db.data but only in the status for the 'name' cell (which is in both tables) is set to 'n'. 这里,名称是相同的,但我想从db.data中提取的数据,但只有在'name'单元格(在两个表中)的状态设置为'n'。

From what I understand, the data in each name column is the same for both tables. 据我所知,每个name列中的数据对于两个表都是相同的。 So a JOIN should work here: 所以JOIN应该在这里工作:

SELECT db.data.*
FROM db.data
INNER JOIN db.info ON db.data.name = db.info.name
WHERE db.info.status = 'n'

You can use a join on name between the two tables 您可以在两个表之间使用名称连接

SELECT d.name, d.date
FROM db.data d
    JOIN db.info i ON d.name = i.name
WHERE i.status = 'N'
ORDER BY d.date

I think this is what you want: 我想这就是你想要的:

select d.name, d.dateadded
  FROM db.info i, db.data d 
  WHERE i.name = d.name
    AND i.status = 'n';

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

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