How to get all potential orders from a mysql table - mysql

I have a mysql query and can't quite figure out how to pull up all potential customers for a certain product_id from a given country_id. I want a result that has just one record per customer.
My tables are orders and customers.
---customers--
customer_id
country_id
name
---orders---
order_id
product_id
customer_id
My current query below gives too many results. Namely records from other product_id's that are not = 1. Here is my query:
SELECT o.order_id
, c.name
, c.customer_id
, c.country_id
, o.product_id
FROM customers c
LEFT
JOIN orders o
ON o.customer_id = c.customer_id
WHERE o.product_id = 1
OR c.country_id = 1
Some sample results might look like this:
order_id name customer_id country_id product_id
1 Joe Smith 1 1 1
2 Joe Smith 1 1 2
3 John Doe 2 1 1
4 Kirk Smith 3 1 1
NULL Ron Rhoden 6 1 1
NULL Sam Smith 7 1 1
For my purposes you can assume that a given customer will only order a given product once. Notice how I get a result for Joe Smith with product_id=2. I don't want that result in my list. The Ron Rhoden and Sam Smith results are desirable for my purposes. How do I filter the product_id<>1 records but still include all the country_id=1 records?
Thanks.

This is my attempt:
SELECT o.order_id
, c.name
, c.customer_id
, c.country_id
, o.product_id
FROM customers c
LEFT
JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IN (SELECT order_id FROM orders WHERE product_id = 1)
OR c.country_id IN (SELECT customers.country_id FROM customers WHERE product_id = 1)
);

If I understand correctly, you want all customers from country "1" that have ordered product "1". I would suggest using exists:
select c.*
from customers c
where c.country_id = 1 and
exists (select 1
from orders o
where o.customer_id = c.customer_id and
o.product_id = 1
);

Ok, I think this works. A bit complicated but I first asked for all the product_id=1 records from orders then did a UNION query and then asked for everyone in customers with country_id=1 but NOT IN the previous product_id=1 results.
SELECT o.order_id, c.name, c.customer_id, c.country_id, o.product_id
FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.product_id = 1 UNION SELECT NULL AS order_id, c.name, c.customer_id, c.country_id, NULL AS product_id
FROM customers c
WHERE c.country_id = 1 AND c.customer_id NOT IN (SELECT c2.customer_id FROM customers c2 INNER JOIN orders o2 ON c2.customer_id=o2.customer_id WHERE o2.product_id=1)

Related

Join Tables - include in result rows without data in other table

I have this sql:
SELECT
c.id,
c.name,
SUM(CASE WHEN o.status = 1 THEN 1 ELSE 0 END)
FROM orders o, customers c
WHERE o.customer_id = c.id
GROUP BY c.id;
Take all customers from table customer.
Count how much rows have column status=1 in table orders, grouped by customer_id.
My sql works perfect if customer have at least one order in table orders(any status, even if all have status different by 1).
But if there is an customer which have no order(does not depend status) in table order, it will not appear in result.
Let's say we have those 2 customers:
100 - C1 - 15 orders(7 have status 1)
101 - C2 - No orders
The Result will be:
100 | C1 | 7
How I can include C2?
100 | C1 | 7
101 | C2 | 0
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
And, you want a LEFT JOIN:
SELECT c.id, c.name,
COUNT(o.customer_id)
FROM customers c LEFT JOIN
orders o
ON o.customer_id = c.id AND o.status = 1
GROUP BY c.id;
You need a left join:
SELECT id, name, COALESCE(sum_orders, 0)
FROM customer c
LEFT JOIN (SELECT customer_id, SUM(CASE WHEN o.status = 1 THEN 1 ELSE 0 END) AS sum_orders
FROM orders
GROUP BY customer_id) o ON c.id = o.customer_id

Mysql query with count(distinct) show nothing if group by has no result

I have a query on 3 tables, settlement, order and passenger.
A settlement has orders, and orders has passengers.
TABLE: Settlemnt
id
TABLE: Order
id settlement_id
TABLE: passenger
id orders_id
If settlement has no orders (and then no passengers), it returns nothing.
What I want, is to return the empty settlement even if it has no orders.
It is the count(passengers) that ruins it for me and returns no settlement if empty.
This is the simplified query:
select s.id, o.id, count(distinct p.id)
from settlement s
left join orders o ON o.settlement_id = s.id
left join passenger p on p.orders_id = o.id
where s.date = '2016-02-02';
group by o.id
How can I make it return settlements with no orders?
Works for me...
SELECT s.id s_id
, o.id o_id
, p.id p_id
FROM settlement s
LEFT
JOIN orders o
ON o.sid = s.id
LEFT
JOIN passengers p
ON p.oid = o.id;
+------+------+------+
| s_id | o_id | p_id |
+------+------+------+
| 1 | NULL | NULL |
| 2 | 1 | 1 |
+------+------+------+
http://sqlfiddle.com/#!9/9a01b/4
The major problem with your query appears to be that you GROUP BY the o.id. Any settlement that has no order will have an o.id of NULL, hence all settlements with no orders will be lumped together into one row. Which s.id that is returned for this o.id is not defined (and effectively random).
Change it to GROUP BY both s.id and o.id
SELECT s.id,
o.id,
COUNT(DISTINCT p.id)
FROM settlement s
LEFT JOIN orders o ON o.settlement_id = s.id
LEFT JOIN passenger p ON p.orders_id = o.id
WHERE s.date = '2016-02-02';
GROUP BY s.id,
o.id

MySQL join table return only one match

I'm struggling the the JOIN in a MySQL query. Somehow I can't find out why my result is not what I want.
I have two tables, a table orders and a table products. The table product holds the order.id of the order. So a order can have more than one products, so for example the table products holds two records for a order.
The result I need is all orders where a product holds a VAT of 21.
Table example.
orders
id | customer
---------------
1 | John Doe
2 | Hello World
order_products
id | order_id | product | vat
1 | 1 | Porsche 911 GT4 | 21
2 | 1 | Audi R8 LMS | 21
3 | 1 | Ferrari Enzo | 19
3 | 2 | Bugatti Veyron | 19
No I want all orders where the products have a VAT of 21. So I will do a LEFT JOIN on the table order_products:
SELECT orders.id, orders.customer, order_products.product FROM orders LEFT JOIN order_products ON orders.id = order_products.order_id WHERE order_products.vat = '21'
This returns the following:
1 John Doe Porsche
1 John Doe Audi R8 LMS
But I only need one result because the orders.id is important for me, not all products in the order. I only join on the order_products to get the orders with only VAT 21. At the moment I ran out of options on how to fix this. Even after reading several topics about joins on this site and other sites.
First, you aren't going to return any orders that don't have products, so there is no need for a left join...an inner join is fine.
If the orders_product is not important to you, you can use a subquery and not select any columns from the orders_product. With your current query, you're selecting a column though.
Something like...
SELECT id, customer
FROM orders
WHERE order_id IN (SELECT order_id FROM order_products
WHERE order_products.vat = '21');
If you prefer not to use a correlated subquery, you can use a group by or distinct
SELECT orders.id, orders.customer
FROM orders
INNER JOIN order_products ON orders.id = order_products.order_id
WHERE order_products.vat = '21'
GROUP BY orders.id, orders.customer;
or...
SELECT DISTINCT orders.id, orders.customer
FROM orders
INNER JOIN order_products ON orders.id = order_products.order_id
WHERE order_products.vat = '21';
If you only care about the orders then you can group by the order id:
SELECT orders.id, orders.customer, order_products.product
FROM orders
LEFT JOIN order_products ON orders.id = order_products.order_id
WHERE order_products.vat = '21'
GROUP BY orders.id;
However note that order_products.product will only be one of the products. If you wish to display all of the products but in the one column you can user GROUP_CONCAT:
SELECT orders.id, orders.customer, GROUP_CONCAT(order_products.product)
FROM orders
LEFT JOIN order_products ON orders.id = order_products.order_id
WHERE order_products.vat = '21'
GROUP BY orders.id;
This will return:
1 John Doe Porsche, Audi R8 LMS
SELECT DISTINCT o.id
, o.customer
FROM orders o
JOIN order_products op
ON op.order_id = o.id
WHERE op.vat = 21;

Subquery select as filed

I tried to make a subselect in a MySQL selct statement e.g. like the following one:
SELECT
c.id AS id,
c.name AS name,
(SELECT count(*) FROM orders o WHERE o.user_id = c.id) AS order_count
FROM
customers c
Any ideas why this does not work in MySQL?
Is it possible to do something like that in MySQL?
Tried it in Version 3.23.58 and 5.1.60.
Thanks in advance!
You missed , after name, try this:
SELECT
c.id AS id,
c.name AS name,
(SELECT count(*) FROM orders o WHERE o.user_id = c.id) AS order_count
FROM
customers c
In order to avoid these sorts of errors (like missing commas), I like to write queries out like this...
SELECT c.id
, c.name
, COALESCE(COUNT(o.user_id),0) order_count
FROM customers c
LEFT
JOIN orders o
ON o.user_id = c.id
GROUP
BY c.id;
SELECT
c.id AS id ,
c.name AS name ,
(SELECT count(*)
FROM orders o
WHERE o.user_id = c.id) AS order_count
FROM
customers c
Use ',' after every column
Another method using joins
SQLFIDDLE DEMO
Sample data:
ID NAME
1 john
2 jack
3 kelly
ID USERID
10 1
11 2
12 1
13 3
14 2
15 3
16 2
17 4
18 1
Query:
SELECT count(o.userid), c.id, c.name
FROM cusomter c
LEFT JOIN orders o
ON c.id = o.userid
GROUP BY c.id
;
You may add case when to represent null as a 0 if a customer doesen't have any orders. You choice.
Results:
COUNT(O.USERID) ID NAME
3 1 john
3 2 jack
2 3 kelly

MySQL query - Join Query

Not sure how to write this join for 2 tables. Here is some sample data to illustrate:
orders
-----------------------
| order_id | customer |
-----------------------
ABC12345 1
ABC12346 4
ABC12347 3
ABC12348 2
ABC12349 2
ABC12350 3
customers
-----------------------------------
| id | name | email |
-----------------------------------
1 James james#gmail.com
2 Alice alice#hotmail.com
3 Jimbo james#gmail.com
4 Jim james#gmail.com
5 Lucy lucy#yahoo.com
I have an order_id, which I already know. Let's use the first one in the table: ABC12345. As you can see, the customer ID is 1, so that order was placed by James. Now sometimes James has ordered again using different names but we know it's him because of his email address.
So how do I retrieve all of James' orders based on his email address of james#gmail.com, if I know one of his order numbers (ABC12345)?
Edit: Not sure I stressed this enough... James has ordered 3 times, using the same email address but names of James, Jim and Jimbo. I need all of his orders using james#gmail.com as the email address.
SELECT o2.order_id
FROM orders o1
INNER JOIN customers c1
ON o1.customer = c1.id -- get the customer for the first order
INNER JOIN customers c2
ON c1.email = c2.email -- find all customers with same email
INNER JOIN orders o2
ON c2.id = o2.customer -- find all orders for those customers
WHERE o1.order_id = 'ABC12345'
You can use this:
SELECT order_id
FROM orders
WHERE customer IN (
SELECT id
FROM customers
WHERE email = (SELECT c.email FROM customers c JOIN orders o ON c.id = o.customer WHERE o.order_id = 'ABC12345')
)
You need to first quantify the person of the order to the customer table... Then, get all customer IDs by that same email... THEN get the orders that qualify those customer IDs.
select o2.*
from orders o2
JOIN ( select c2.ID
from customers c2
join ( select c1.email
from orders o
join customers c1
on o.Customer = c1.ID
where
o.order_id = 'ABC12345' ) FoundTheEmail
on c2.email = FoundTheEmail.email
) as SameCustomerEMail
on o2.Customer = SameCustomerEMail.ID
SELECT order_id
FROM orders
WHERE customer IN (SELECT customer
FROM orders
WHERE order_id = 'ABC12345')
try this since james has only one order
SELECT orders.order_id, customers.name, customers.email
FROM orders
INNER JOIN customers ON customers.id = orders.customer
WHERE orders.orer_id = 'ABC12345'
The order ID is a UNIQUE number, then you will not have two orders with the same ID, why care about the customer name?
"Every order will have a Customer's ID."