简体   繁体   中英

Combine queries to grab foreign key value from different able based on result in mssql

I have to work with a mssql database that I have no control over, so sadly, I can't change the structure at all. This database is setup so that there are 2 tables Entry and Area . In the Area table, there is a column sArea that I need to look up based on a value ixEntry . In the Entry table, I can do a look up (the variables are PHP variables):

SELECT sTitle,ixCategory,ixArea FROM Entry WHERE ixEntry='$ixEntry'

and then do a second query

SELECT sArea FROM Area WHERE ixArea='{$return['ixArea']}'

Which works just fine, except with the way that the network is setup, there is considerably more overhead time with two queries.

How can I combine these two queries so that I have a result that would be the equivalent of SELECT sTitle,ixCategory,sArea FROM Entry WHERE ixEntry='$ixEntry' as if sArea were in the Entry table, not ixArea?

  SELECT a.sArea FROM Entry e
        INNER JOIN Area a ON e.ixArea = a.ixArea
            WHERE e.ixEntry='$ixEntry'

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