简体   繁体   English

多表的SQL查询

[英]SQL QUERY ON MULTIPLES TABLES

I'm new in Pure Sql I want to write this as Query 我是Pure Sql的新手,我想将其编写为Query

select items.* 
from items 
LEFT OUTER JOIN 
    (select sum(purchase_details.quantity) as total 
    from purchase_details 
    where (purchase_details.item_id=items.id)  
GROUP BY purchase_details.item_id) ABC 

but this gives an error 但这给出了一个错误

 You have an error in your SQL syntax; check the manual that corresponds to 
    your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' 
    at line 1

i don't know why it's not working 我不知道为什么它不起作用

The syntax error here is that you need an on -clause for your left join . 这里的语法错误是,你需要一个on -clause您left join But the underlying conceptual problem is different: you cannot join with a dependent subquery . 但是底层的概念性问题有所不同:您不能与从属子查询一起join

You can fix your query like this: 您可以这样修改查询:

select items.* 
from items 
LEFT OUTER JOIN (
  select item_id, sum(purchase_details.quantity) as total
  from purchase_details 
  GROUP BY purchase_details.item_id
) ABC on ABC.item_id = items.id;

This moved your inner where -condition (that would depend on items.id , which is not allowed, as it is out of scope) to the on -clause. 这将您的内部where items.id (这将取决于items.id ,这是不允许的,因为它超出范围)移到了on子句上。 Thus item_id is also added in the inner select (as it is needed outside). 因此, item_id也被添加到内部select (因为在外部需要)。

A different way to write this would be 一种不同的写法是

select items.*, 
   (select sum(purchase_details.quantity) 
    from purchase_details 
    where purchase_details.item_id=items.id) as total
from items;

Here you have a dependent subquery : the inner where -clause depends on the outer items.id . 这里有一个依赖子查询 :内where -clause依赖于外items.id You do not need a group by anymore, as the where -condition already uses just the rows for that item. 您不再需要group by ,因为where -condition已经仅使用了该项目的行。 (And you can also only return at most one row in this context anyway.) (在这种情况下,您最多也只能返回一行。)

Both queries are equivalent, and can (if the optimizer finds that execution plan) internally actually be executed in exactly the same way (which is nothing you have to care about much though, as long as you provide appropriate indexes). 这两个查询是等效的,并且可以(如果优化程序找到了执行计划)在内部实际上以完全相同的方式执行(只要您提供适当的索引,就不必在乎什么了)。

So in your case you can use both (and maybe check which one is faster); 因此,在您的情况下,您可以同时使用这两种方式(并检查哪种方式更快); if you want to get additional information for that item, you should prefer the left join -version though, eg use 如果要获取该项目的其他信息,则应首选left join -version,例如,使用

...
LEFT OUTER JOIN (
  select item_id, 
    sum(purchase_details.quantity) as total,
    count(purchase_details.item_id) as cnt,
    max(purchase_details.quantity) as max_quantity_per_order,
    max(purchase_details.date) as latest_order,
    ...
  from purchase_details 
  GROUP BY purchase_details.item_id
) ABC on ABC.item_id = items.id;

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

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