如何起诉子查询

发布于 2024-12-02 08:16:56 字数 1543 浏览 0 评论 0原文

  $logged = $_SESSION['logged'];

  $construct =  "SELECT child.* 
                 FROM products child 
                 LEFT JOIN products parent on parent.sid=child.sid 
                 WHERE parent.id =
                   '(SELECT productid 
                     FROM subscribed 
                     WHERE username=\"$logged\")' 
                 AND parent.keyword != child.name 
                 ORDER BY child.id DESC";  

我无法让子查询工作,但得到了错误的结果。父查询很好,因为我以前用过它。

我希望子查询从表“订阅”中获取所有产品 ID,其中 username=$logged。

这就是我想要的,但在一个查询中,检查 WHERE 子句是否存在差异。

$construct =  "SELECT child.* 
               FROM products child 
               LEFT JOIN products parent on parent.sid=child.sid 
               WHERE parent.id= 
                 'SUB QUERY RESULT 1' 
               AND parent.keyword != child.name 
               ORDER BY child.id DESC"; 

$construct =  "SELECT child.* 
               FROM products child 
               LEFT JOIN products parent on parent.sid=child.sid 
               WHERE parent.id=
                 'SUB QUERY RESULT 2' 
               AND parent.keyword != child.name 
               ORDER BY child.id DESC"; 

$construct =  "SELECT child.* 
               FROM products child 
               LEFT JOIN products parent on parent.sid=child.sid 
               WHERE parent.id=
                 'SUB QUERY RESULT 3' 
               AND parent.keyword != child.name 
               ORDER BY child.id DESC"; 

如何在一个查询中执行此操作而不循环查询。我还需要子查询吗?

  $logged = $_SESSION['logged'];

  $construct =  "SELECT child.* 
                 FROM products child 
                 LEFT JOIN products parent on parent.sid=child.sid 
                 WHERE parent.id =
                   '(SELECT productid 
                     FROM subscribed 
                     WHERE username=\"$logged\")' 
                 AND parent.keyword != child.name 
                 ORDER BY child.id DESC";  

I'm having trouble getting the subquery to work i get the wrong results. The parent query is fine because i have used it before.

I want the sub query to get all productids from the table 'subscribed' where username=$logged.

This is what i want but in one query, check WHERE clause for differences.

$construct =  "SELECT child.* 
               FROM products child 
               LEFT JOIN products parent on parent.sid=child.sid 
               WHERE parent.id= 
                 'SUB QUERY RESULT 1' 
               AND parent.keyword != child.name 
               ORDER BY child.id DESC"; 

$construct =  "SELECT child.* 
               FROM products child 
               LEFT JOIN products parent on parent.sid=child.sid 
               WHERE parent.id=
                 'SUB QUERY RESULT 2' 
               AND parent.keyword != child.name 
               ORDER BY child.id DESC"; 

$construct =  "SELECT child.* 
               FROM products child 
               LEFT JOIN products parent on parent.sid=child.sid 
               WHERE parent.id=
                 'SUB QUERY RESULT 3' 
               AND parent.keyword != child.name 
               ORDER BY child.id DESC"; 

How do I do this in one query without looping the query. Do I even need a subquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-12-09 08:16:56

您可以使用内部联接来执行您想要的操作:

 $construct = "SELECT child.*         
   FROM products child         
   LEFT JOIN products parent on parent.sid=child.sid         
   INNER JOIN subscibed s ON (s.productid = parent.id) 
   WHERE s.username = '$logged' 
     AND parent.keyword != child.name         
   ORDER BY child.id DESC";  

如果您希望将三个查询合并为一个,请执行以下操作:

$construct =  "SELECT child.*              
           FROM products child              
               LEFT JOIN products parent on (parent.sid = child.sid)              
           WHERE parent.id IN             
                 (SELECT productid FROM table1 WHERE username= '$logged'
                UNION 
                  SELECT productid FROM table2 WHERE something = '$somethingelse'
                UNION 
                  SELECT productid FROM table3 WHERE otherthing = '$otherstuff'
                 )              
           AND parent.keyword != child.name              
           ORDER BY child.id DESC"; 

You can use an inner join to do what you want:

 $construct = "SELECT child.*         
   FROM products child         
   LEFT JOIN products parent on parent.sid=child.sid         
   INNER JOIN subscibed s ON (s.productid = parent.id) 
   WHERE s.username = '$logged' 
     AND parent.keyword != child.name         
   ORDER BY child.id DESC";  

If you want the three queries join in one do:

$construct =  "SELECT child.*              
           FROM products child              
               LEFT JOIN products parent on (parent.sid = child.sid)              
           WHERE parent.id IN             
                 (SELECT productid FROM table1 WHERE username= '$logged'
                UNION 
                  SELECT productid FROM table2 WHERE something = '$somethingelse'
                UNION 
                  SELECT productid FROM table3 WHERE otherthing = '$otherstuff'
                 )              
           AND parent.keyword != child.name              
           ORDER BY child.id DESC"; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文