Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phrasing SQL that has subquery returning multiple rows

Tags:

sql

mysql

I am tring to query a set of data from mySQL with the following SQL:

SELECT * FROM orders 
WHERE datediff((now()), (SELECT datepurchased FROM orders WHERE email = '[email protected]')) <= 1

But the problem is I get the error that #1242 - Subquery returns more than 1 row because the statement SELECT datepurchased FROM orders WHERE email = '[email protected]' is returning more than one rows, which is perfectly correct.

The idea that I wanted is to select from the orders table such that the date difference between the current time and the datepurchased field is <=1 and that the email field is equals to [email protected].

There could be many rows in the orders table with their email fields as [email protected]. I just want to find out all of these rows with email fields equals [email protected] that have datepurchased less than or equals one.

How should I phrase my SQL to achieve this?

like image 689
Carven Avatar asked Aug 09 '26 11:08

Carven


1 Answers

There's no need for a subquery in this case. Simply:

SELECT *
    FROM orders
    WHERE email = '[email protected]'
        AND DATEDIFF(NOW(), datepurchased) <= 1;
like image 69
Joe Stefanelli Avatar answered Aug 11 '26 05:08

Joe Stefanelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!