简体   繁体   English

在SQL中使用INNER JOIN左联接

[英]Left Join with INNER JOIN in SQL

I have a question that I need to solve: I have three tables that join with inner join. 我有一个需要解决的问题:我有三个通过内部联接联接的表。 However in this three table called "products","shopping cart","purchase" , I need to do a left join for have the lists of the user that haven't bought in the shop sistem. 但是,在这三个称为"products","shopping cart","purchase" ,我需要做一个左联接,以获取尚未在商店系统中购买的用户的列表。

For this I tried to do a left join in the entity "products" -> "shopping cart" and after that i tried a inner join beetwen purchase and shopping cart 为此,我尝试在实体"products" -> "shopping cart"进行左联接,然后尝试进行内部联接beetwen购买和购物车

RESULT? 结果? Nothing one (the system ignore the request). 一无所有(系统将忽略该请求)。

My goal is to have returned all purchases NOT FACTS by users in a given time interval. 我的目标是在给定的时间间隔内退还用户的所有购买记录。

For semplify all,I enclose a copy of my Database with photo. 为了简化起见,我附上一张带照片的数据库副本。

SELECT prodotti.nome_prodotto, carrello.quantita, acquisto.data_acquisto
FROM    (   subquery.prodotti prodotti
LEFT JOIN subquery.carrello carrello
    ON (prodotti.id_prodotto = carrello.id_prodotto))
        JOIN subquery.acquisto acquisto
        ON (acquisto.id_acquisto = carrello.id_acquisto)

I have tried this solutions: 我已经尝试过以下解决方案:

SELECT prodotti.nome_prodotto, acquisto.data_acquisto, carrello.quantita   
FROM    ( prodotti 
            LEFT JOIN carrello 
            ON prodotti.id_prodotto = carrello.id_prodotto )        
INNER JOIN           acquisto 
    ON acquisto.id_acquisto = carrello.id_acquisto

AND

SELECT prodotti.nome_prodotto, acquisto.data_acquisto, carrello.quantita
FROM ( acquisto 
        INNER JOIN carrello 
            ON acquisto.id_acquisto = carrello.id_acquisto)
LEFT JOIN prodotti 
    ON prodotti.id_prodotto = carrello.id_prodotto

BUT NOTHING...I return a result like inner join 但是没什么...我返回类似内部联接的结果

PS: there you can find a copy of database and the toad file PS:您可以在其中找到数据库和Toad文件的副本

http://www.ricetteingironelweb.it/Desktop.zip http://www.ricetteingironelweb.it/Desktop.zip

在所有INNER JOIN之后,尝试进行LEFT JOIN

EDIT : After reading your comment, I understand the question a bit more - It sounds like you're trying to see data on items that are in the cart, but haven't been purchased in X amount of time. 编辑 :阅读您的评论后,我对问题的理解有所增加-听起来您正在尝试查看购物车中商品的数据,但尚未在X时间内购买。

To do this, LEFT JOIN the purchase table, and then use the WHERE clause to limit the results to a specific difference in time. 为此,请LEFT JOIN purchase表,然后使用WHERE子句将结果限制为特定的时间差。 Since we're using a left join, the purchase table won't always have a value, so in those cases we use IFNULL to specify today's date for the math (if the item in the cart still hasn't been purchased, figure out how many days from today that it's been in the cart). 由于我们使用的是左IFNULL ,因此购买表并不总是具有值,因此在这种情况下,我们使用IFNULL来指定今天的数学日期(如果尚未购买购物车中的商品,请弄清楚从今天起有多少天都在购物车中)。

SELECT /*products in cart*/
    prodotti.nome_prodotto
    ,carrello.quantita
FROM
    prodotti 
    INNER JOIN carrello 
        ON prodotti.id_prodotto = carrello.id_prodotto
    LEFT JOIN acquisto
        ON acquisto.id_prodotto = carrello.id_prodotto
WHERE
    /* show only items over a certain age - 5 days in this example */
    DATEDIFF((ifnull(acquisto.id_acquisto,NOW()) /* use today's date if purchase doesn't exist */
        - carrello.id_acquisto) > 5) 

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

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