我在此示例中遇到查询问题:
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表中选择行等于prekes_main row by key charak_pre.pr_id和prekes_main.pre_id。
答案 0 :(得分: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
答案 1 :(得分: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