1

询问:

SELECT * FROM #__content WHERE featured=1 and catid=$category_id or catid IN 
( SELECT id FROM #__categories WHERE parent_id=$category_id )

Featured=1只在这里工作catid=$category_id。开始此catid IN ( SELECT id FROM #__categories WHERE parent_id=$category_id )输出所有行的位置,并且在此位置忽略 features=1。如何添加featured=1条件catid IN

所以基本上它现在看起来像这样:

$query = "SELECT * FROM #__content WHERE featured=1 and catid=$category_id";
$query2 = "SELECT * FROM #__content WHERE catid IN 
    ( SELECT id FROM #__categories WHERE parent_id=$category_id )";
4

2 回答 2

0

加盟怎么样?

$query = "SELECT * FROM #__content WHERE featured=1 and catid=$category_id";
$query2 = "
SELECT * FROM #__content as con
LEFT JOIN #__categories as cat
on con.cat_id =cat.category_id
WHERE cat.parent_id=$category_id;"
于 2013-01-18T11:22:35.887 回答
0

您需要添加括号来强制执行您需要的评估顺序:

SELECT * FROM #__content WHERE catid=$category_id OR (featured=1 AND catid IN 
( SELECT id FROM #__categories WHERE parent_id=$category_id ))

由于AND优先级高于OR,因此不带括号的表达式在(featured=1 and catid=$category_id)为真或catidIN子查询指定的列表中时为真。

于 2013-01-18T11:17:28.533 回答