[英]Pull back rows from multiple tables with a sub-select?
I have a script which generates queries in the following fashion (based on user input): 我有一个脚本,该脚本以以下方式(基于用户输入)生成查询:
SELECT * FROM articles
WHERE (articles.skeywords_auto ilike '%pm2%')
AND spubid IN (
SELECT people.spubid FROM people
WHERE (people.slast ilike 'chow')
GROUP BY people.spubid)
LIMIT 1;
The resulting data set: 结果数据集:
Array ( [0] =>
Array (
[spubid] => A00603
[bactive] => t
[bbatch_import] => t
[bincomplete] => t
[scitation_vis] => I,X
[dentered] => 2009-07-24 17:07:27.241975
[sentered_by] => pubs_batchadd.php
[drev] => 2009-07-24 17:07:27.241975
[srev_by] => pubs_batchadd.php
[bpeer_reviewed] => t
[sarticle] => Errata: PM2.5 and PM10 concentrations from the Qalabotjha low-smoke fuels macro-scale experiment in South Africa (vol 69, pg 1, 2001)
[spublication] => Environmental Monitoring and Assessment
[ipublisher] =>
[svolume] => 71
[sissue] =>
[spage_start] => 207
[spage_end] => 210
[bon_cover] => f
[scover_location] =>
[scover_vis] => I,X
[sabstract] =>
[sabstract_vis] => I,X
[sarticle_url] =>
[sdoi] =>
[sfile_location] =>
[sfile_name] =>
[sfile_vis] => I
[sscience_codes] =>
[skeywords_manual] =>
[skeywords_auto] => 1,5,69,2001,africa,assessment,concentrations,environmental,errata,experiment,fuels,low-smoke,macro-scale,monitoring,pg,pm10,pm2,qalabotjha,south,vol
[saward_number] =>
[snotes] =>
) )
The problem is that I also need all the columns from the 'people' table (as referenced in the sub select) to come back as part of the data set. 问题是,我还需要“人员”表中的所有列(在子选择中引用)才能作为数据集的一部分返回。 I haven't (obviously) done much with sub selects in the past so this approach is very new to me.
我过去(显然)没有对子选择做很多事情,因此这种方法对我来说是非常新的。 How do I pull back all the matching rows/columns from the articles table AS WELL as the rows/column from the people table?
如何从商品表AS WELL中拉回所有匹配的行/列,以及从人员表中拉回所有匹配的行/列?
Are you familiar with joins? 您熟悉联接吗? Using ANSI syntax:
使用ANSI语法:
SELECT DISTINCT *
FROM ARTICLES t
JOIN PEOPLE p ON p.spubid = t.spudid AND p.slast ILIKE 'chow'
WHERE t.skeywords_auto ILIKE'%pm2%'
LIMIT 1;
The DISTINCT saves from having to define a GROUP BY for every column returned from both tables. DISTINCT不必为两个表返回的每个列定义GROUP BY。 I included it because you had the GROUP BY on your subquery;
之所以包含它,是因为您在子查询中包含了GROUP BY。 I don't know if it was actually necessary.
我不知道这是否真的必要。
Could you not use a join instead of a sub-select in this case? 在这种情况下,可以使用联接而不是子选择吗?
SELECT a.*, p.*
FROM articles as a
INNER JOIN people as p ON a.spubid = p.spubid
WHERE a.skeywords_auto ilike '%pm2%'
AND p.slast ilike 'chow'
LIMIT 1;
Lets start from the beginning 让我们从头开始
To see the contents of the inner table, you actually have to join it. 要查看内部表的内容,您实际上必须将其联接。 The contents are not exposed unless it shows up in the from section.
除非在“发件人”部分中显示内容,否则不会公开内容。 A left outer join from the people table to the articles table should be equivalent to an IN query :
从人员表到商品表的左外部联接应等效于IN查询:
SELECT * FROM people LEFT OUTER JOIN articles ON articles.spubid = people.spubid WHERE people.slast ilike 'chow' AND articles.skeywords_auto ilike '%pm2%' LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.