简体   繁体   English

选择所有项目cat_id = x或parent_id = x(父ID不同的表)

[英]Select all items Where cat_id= x or parent_id = x (Parent id different table)

Hi as the title say i have a php code where i sucefully echo the articles in a table, the problem is in this table i just have cat_id the parent_id is referenced in other table.. 嗨,因为标题说我有一个PHP代码,我sucefully回应表中的文章,问题是在这个表中我只有cat_id parent_id在其他表中被引用..

"articulos" table

id | titulo | fecha_evento | descripcion | img | cat_id

"categoriablog" table

    id | parent_id 

This is the way im making my query 这是我进行查询的方式

$query1 = "SELECT id,titulo,fecha_evento,descripcion,img FROM articulos WHERE cat_id = 1";
    $result = mysql_query($query1);

My goal is to do something like this, but "parent_id" is in other table 我的目标是做这样的事情,但“parent_id”在其他表中

$query1 = "SELECT id,titulo,fecha_evento,descripcion,img FROM articulos WHERE cat_id = 1 OR parent_id = 1";
        $result = mysql_query($query1);

JOIN the two tables: JOIN两个表:

SELECT 
  a.id, a.titulo, a.fecha_evento, a.descripcion, a.img 
FROM Article AS a 
INNER JOIN Category AS c ON a.cat_id = c.id
WHERE a.cat_id = x
   OR c.parent_id = x

You can use a sub query 您可以使用子查询

SELECT id,titulo,fecha_evento,descripcion,img FROM Article
 WHERE cat_id = 1 OR cat_id IN
 (SELECT id from Category where parent_id=1)

Edit 编辑

Please note that this solution is a little bit slower than the JOIN solution in the answer by Mahmoud. 请注意,这个解决方案比Mahmoud的答案中的JOIN解决方案慢一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM