Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
am i not getting the sufficient output on this query

What I have tried:

SQL
select * from  product_table p join order_table o  on p.p_id=o.p_id  where order_date <> curdate();
Posted
Updated 28-Feb-19 1:30am
v2
Comments
Maciej Los 28-Feb-19 5:55am    
Please, start using formatting.

Tip: You have to start accepting answers, which resolve your issues. There's unwritten rule of QA board ;)

Surely it should be:

select * from  product_table p join order_table o  on p.p_id=o.p_id where order_date >= curdate();
 
Share this answer
 
In general: you have to get all sold products and exclude them from all products.
But i don't know how your database look like and what relationships happen between tables.

For example:
SQL
SELECT product_id
FROM Products
WHERE NOT IN(
   SELECT DISTINCT product_id
   FROM Orders);
-- you can add your logic about date of sale:
--   WHERE dateofsale<=curdate()
-- into subquery


For further details, please see:
MySQL :: MySQL 8.0 Reference Manual :: 13.2.11.6 Subqueries with EXISTS or NOT EXISTS[^]
MySQL :: MySQL 8.0 Reference Manual :: 12.3.2 Comparison Functions and Operators: IN()[^]
MySQL :: MySQL 8.0 Reference Manual :: 12.3.2 Comparison Functions and Operators: NOT IN()[^]
 
Share this answer
 
v3
The clause in your query where order_date <> curdate() is going to return all the product and order details except for today's because of the not equals.
The answer to this is to make that a greater-than-or-equal-to.
SQL
SELECT  *
FROM    product_table  p
JOIN    order_table    o  on p.p_id = o.p_id
WHERE   order_date >= curdate()             -- products and orders from today

This will; however, still contain products that were sold in the past. So the next thing to do will be to add in a sub-query to remove the products from the past. To get the Product IDs from past sales will be SELECT p_ID FROM order_table WHERE order_date < curdate()
and this will be added into the original query with a NOT IN clause to remove them
SQL
SELECT  *
FROM    product_table  p
JOIN    order_table    o  on p.p_id = o.p_id
WHERE   order_date >= curdate()             -- Products and orders from today 
AND     p.p_id NOT IN (                     -- remove products from the past
            SELECT  p_ID
            FROM    order_table
            WHERE   order_date < curdate()
       )

Now this query is going to give you all the product details but it is also going to contain all the order details as well. And if there were multiple purchasers of the same product they will return multiple rows for the same product. If we want to reduce this we will remove the JOIN statement and work it in as a sub-query.
SQL
SELECT *
FROM   product_table p     -- Products only, no order information
WHERE  p.p_id IN (         -- Get Products sold today
         SELECT p_ID
         FROM   order_table
         WHERE  order_date >= curdate()
)
AND    p.p_id NOT IN (       -- Remove products from the past
         SELECT p_ID
         FROM   order_table
         WHERE  order_date < curdate()
)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900