如何起诉子查询
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用内部联接来执行您想要的操作:
如果您希望将三个查询合并为一个,请执行以下操作:
You can use an inner join to do what you want:
If you want the three queries join in one do: