SQL 2 Columns of Different Types in Same Table - Working With 'Uknown' Amount
-
24-02-2021 - |
Question
I'm very new to SQL and I've been searching for an answer for a while now, can't seem to find it.
I have 1 table of CustOrders which has 3 columns.
Table CustOrders
- Name (varchar)
- Number_Orders (int)
- order_date (datetime)
Question
I would like to get a list of customers that have had less orders than a specific customer 'x'. How would I go about doing that?
Select Statement
I tried with this logic, which didn't seem to work and is probably all wrong, which was:
SELECT name, number_orders
FROM custOrders
WHERE number_orders < (select number_orders from custOrders where name = 'x');
It returned an empty set, which should not be empty, because I know there are customers there with less orders.
Could you please help me with this?
I am using MariaDB.
Solution
SELECT t1.*
FROM custOrders t1, custOrders t2
WHERE t1.number_orders < t2.number_orders
AND t2.name = 'x';
PS. custOrders.name
must be defined as unique by according index.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange