0

在此示例中,我的查询有问题:

 SELECT pm.name,
       pm.pre_id,
       pm.pgs_id,
       pm.link,
       pm.del,
       pm.maz_kaina,
       pm.int_kaina,
       pm.d1_kaina,
       pm.d2_kaina,
       pm.d3_kaina,
       pm.pvm,
       pm.vnt,
       pm.gamintojas,
       pm.akcija,
       pm.akc_kaina
FROM   `prekes_main` AS pm
       INNER JOIN (SELECT charak_pre.pr_id
                   FROM   charak_pre
                   WHERE  charak_pre.cha_id = 206
                          AND charak_pre.reiksme = 'Parfumuotas vanduo'
                          AND charak_pre.pr_id = pm.pre_id) AS reikiamos_pre0
               ON reikiamos_pre0.pr_id = pm.pre_id
WHERE  pm.pgs_id = '2431'
ORDER  BY pm.del DESC,
          int_kaina ASC
LIMIT  0, 18  

INNER JOIN当我尝试执行此操作时遇到问题。这是我得到的错误:

SQL Error (1054): Unknown column 'pm.pr_id' in WHERE clause.

发生错误的位置是:and charak_pre.pr_id = pm.pr_id。有了这个,我想通过键 charak_pre.pr_id 和 prekes_main.pre_id 从 charak_pre 表中选择等于 prekes_main 行的行。

4

2 回答 2

0

实际上,您可以在没有子查询的情况下执行此操作,因为您没有对以下行执行任何聚合。而是直接将其与表连接charak_pre并将其他条件放在WHERE子句上。

SELECT  DISTINCT pm.*
FROM    prekes_main as pm
        INNER JOIN charak_pre
           ON charak_pre.pr_id = pm.pre_id
WHERE   pm.pgs_id = '2431' AND 
        charak_pre.cha_id = 206 AND 
        charak_pre.reiksme = 'Parfumuotas vanduo'
ORDER   BY pm.del DESC, int_kaina ASC 
LIMIT   0, 18
于 2013-04-08T13:01:06.380 回答
0

我会在没有子查询的情况下编写它,这似乎是一件低效的事情,但我还没有测试过。改为进行普通连接:

SELECT  pm.name, 
        pm.pre_id, 
        pm.pgs_id, 
        pm.link, 
        pm.del, 
        pm.maz_kaina, 
        pm.int_kaina, 
        pm.d1_kaina, 
        pm.d2_kaina, 
        pm.d3_kaina, 
        pm.pvm, 
        pm.vnt, 
        pm.gamintojas, 
        pm.akcija, 
        pm.akc_kaina
FROM    `prekes_main` as pm
        INNER JOIN charak_pre charak_pre  as reikiamos_pre0 
            ON reikiamos_pre0.pr_id = pm.pre_id AND charak_pre.cha_id = 206 and 
                    charak_pre.reiksme = 'Parfumuotas vanduo'
WHERE   pm.pgs_id = '2431' 
ORDER   BY pm.del DESC, 
        int_kaina ASC 
LIMIT 0,18
于 2013-04-08T13:05:35.133 回答