将3个SQL查询合并为1个

时间:2014-02-18 08:41:10

标签: php sql

来自表A中的一行,我需要获取表B中的行,该行在表C中获取一行。 根据我的基本知识,我在查询中查询查询,但我认为有更好的方法可以做到这一点?

<?php  $result = pg_query($dbconn, "
    SELECT sequence, w_homepage_package_id                            
    FROM adempiere.w_homepage_package
    WHERE sequence > 0
    ORDER BY sequence ASC
    "); // first QUERY
    while ($promo = pg_fetch_row($result)) { // first WHILE
        $id = $promo[1];    
        $produktfoto = pg_query($dbconn, "
        SELECT w_bilder_id 
        FROM adempiere.w_homepage_image 
        WHERE w_homepage_package_id = $id LIMIT 1"); // second QUERY
        while ($produktrow = pg_fetch_row($produktfoto)) { // second WHILE
            $bilderid = $produktrow[0];
            $bildurl = pg_query($dbconn, "
            SELECT w_bildurl 
            FROM adempiere.w_bilder 
            WHERE w_bilder_id = $bilderid LIMIT 1"); // third QUERY
            while ($bildpfad = pg_fetch_row($bildurl)) { // third WHILE
                echo $bildpfad[0];
            }
        }
    ;}                                  
    ?>

5 个答案:

答案 0 :(得分:1)

子查询或联接。您可以在相对列上连接三个表,或嵌套它们直到到达第一个表。

SELECT w_bildurl 
FROM adempiere.w_bilder 
WHERE w_bilder_id = 
      (SELECT w_bilder_id 
      FROM adempiere.w_homepage_image 
      WHERE w_homepage_package_id IN 
           (SELECT sequence,  
           w_homepage_package_id                            
           FROM adempiere.w_homepage_package
           WHERE sequence > 0
           ORDER BY sequence ASC)
     LIMIT 1)
LIMIT 1

答案 1 :(得分:1)

尝试此查询,如果需要,请使用加入。

SELECT 
    whp.sequence, 
    whp.w_homepage_package_id 
FROM 
    adempiere.w_homepage_package as whp
    NATURAL JOIN 
        adempiere.w_homepage_image as whi
    NATURAL JOIN 
        adempiere.w_builder as wbr
WHERE 
    whp.sequence > 0
ORDER BY whp.sequence ASC;

答案 2 :(得分:1)

这应该是正确的左连接查询(如果我没有忘记任何事情)

SELECT * 
FROM  adempiere.w_homepage_package 
      LEFT JOIN adempiere.w_homepage_image 
             ON adempiere.w_homepage_package.w_homepage_package_id =  adempiere.w_homepage_image.w_homepage_package_id 
      LEFT JOIN adempiere.w_bilder 
             ON adempiere.w_homepage_image.w_bilder_id = adempiere.w_bilder.w_bilder_id 
WHERE  sequence > 0 
ORDER  BY sequence ASC 

了解JOIN关于产品笛卡尔的想法:

Table1
f1  f2
----------
a   1
b   4
c   5

Table2
f3  f4
----------
a   aa
b   cc

f1  f2  f3  f4
--------------------------
a   1   a   aa
a   1   b   cc
b   4   a   aa
b   4   b   cc
c   5   

Table1 LEFT JOIN Table2 ON f1 = f3

a   1   a   aa
b   4   b   cc
c   5

Table1 INNER JOIN Table2 ON f1 = f3
a   1   a   aa
b   4   b   cc

wikypedia http://en.wikipedia.org/wiki/Join_(SQL)

有一个很好的例子

答案 3 :(得分:0)

使用JOIN关键字,它可以让您的生活更轻松。

在此处查看:http://www.w3schools.com/sql/sql_join.asp

答案 4 :(得分:0)

它应该可以正常工作

$res =  "SELECT A.*, B.w_bilder_id, C.w_bildurl FROM w_homepage_package A, w_homepage_image B,w_bilder C WHERE A.sequence>O  AND B.w_homepage_package_id=A.sequence AND C.w_bildurl=B.w_bilder_id ORDER BY A.sequence ASC"; 
$result = pg_query($dbconn, $res); 
while ($promo = pg_fetch_row($result)) 
{

}