将 SQL 重写为 JOINS 而不是子查询
我在使用联接而不是两个子查询重写此查询时遇到一些麻烦。如果你明白我的意思的话,我在解开它时遇到了问题。
SELECT o.order_id, n.title, c.first_name, t1.name, o.product_id,
(SELECT ttd2.tid FROM term_data ttd2, term_node ttn2 WHERE ttd2.vid = 5 AND ttn2.nid = p.nid AND ttd2.tid=ttn2.tid) AS tid,
(SELECT ttd4.name FROM term_data ttd4, term_node ttn4 WHERE ttd4.vid = 8 AND ttn4.nid = p.nid AND ttd4.tid=ttn4.tid) AS month
FROM orders o, products p, node n, customers c, term_data t1, term_node t2
WHERE o.product_id = p.nid
AND p.nid = n.nid
AND o.customer_email = c.customer_email
AND t2.tid = t1.tid
AND t1.vid = 6
AND n.nid = t2.nid
你能帮忙吗?或者给出一些线索/提示。
I have some troubles re-writing this query to using joins instead of the two subqueries. I have a problem untangling it, if you know what i mean.
SELECT o.order_id, n.title, c.first_name, t1.name, o.product_id,
(SELECT ttd2.tid FROM term_data ttd2, term_node ttn2 WHERE ttd2.vid = 5 AND ttn2.nid = p.nid AND ttd2.tid=ttn2.tid) AS tid,
(SELECT ttd4.name FROM term_data ttd4, term_node ttn4 WHERE ttd4.vid = 8 AND ttn4.nid = p.nid AND ttd4.tid=ttn4.tid) AS month
FROM orders o, products p, node n, customers c, term_data t1, term_node t2
WHERE o.product_id = p.nid
AND p.nid = n.nid
AND o.customer_email = c.customer_email
AND t2.tid = t1.tid
AND t1.vid = 6
AND n.nid = t2.nid
Can you help? Or give some clues/hints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ANSI SQL-92 语法重写它(即,使用 JOIN 和 ON 子句),它应该更清晰。
现在,所有
JOIN
和WHERE
子句都混合在一起,因此不容易看出其中的关系。子查询不一定是问题;一旦语法清理完毕,这应该会变得更加清晰。Rewrite it using ANSI SQL-92 syntax (i.e., using JOIN and ON clauses), and it should be much more clear.
Right now all of your
JOIN
andWHERE
clauses are mixed together, so it is not easy to see the relationships. Subqueries are not necessarily a problem; this should become more clear once the syntax is cleaned up.重写为使用 ANSI-92 SQL 并稍微简化以删除冗余连接,您的查询应如下所示:
Rewritten to use ANSI-92 SQL and slightly simplified to remove redundant joins, your query should look something like this:
通常:
将您的
SELECT
列移至主SELECT
语句,将FROM
子句移至JOIN
原始FROM
下方的 > 子句,以及该行的连接条件。您的WHERE
子句可以保持原样。另外,正如 @RedFilter 所说,使用
JOIN
和ON
子句。我认为你正在做笛卡尔,但由于语法我不确定。例如(我不知道这对于您的表结构是否有效,因为您没有给出):
As a rule:
Move your
SELECT
ed columns up to the mainSELECT
statement, move yourFROM
clause to aJOIN
clause below the originalFROM
, and your join condition to that line as well. YourWHERE
clause can stay as is.Also as @RedFilter says, use
JOIN
andON
clauses. I think you are doing cartesians but I'm not sure due to the syntax.For example (I don't know if this is valid for your table structure since you don't give it):