简体   繁体   中英

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.

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

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

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.

To do this, LEFT JOIN the purchase table, and then use the WHERE clause to limit the results to a specific difference in time. 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).

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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