views:

159

answers:

3

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?

+1  A: 

Are you familiar with joins? Using ANSI syntax:

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. I included it because you had the GROUP BY on your subquery; I don't know if it was actually necessary.

OMG Ponies
Wound up settling on this solution: SELECT * FROM articles AS a INNER JOIN people AS p ON a.spubid = p.spubid INNER JOIN status as s on a.spubid = s.spubid WHERE a.skeywords_auto ilike '%pm2%' AND p.saffil = 'DAS' AND p.slast ilike '%Chow%' AND s.iyear >= 2000;
Nicholas Kreidberg
A: 

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;
Jon
The parenthesis used in the JOIN and WHERE clause aren't necessary.
OMG Ponies
Edited. Although I usually keep them on the ON clause.
Jon
A: 

Lets start from the beginning:

  • You shouldn't need a group by. Use distinct instead (you aren't doing any aggregating in the inner query).
  • 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:

    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

krdluzni