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
Related
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
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)
The intersect keyword is not available in mysql. I want to know how to implement the following in mysql db. My tables are:
customer(cid,city,name,state)
orders(cid,oid,date)
product(pid,price,productname)
lineitem(lid,pid,oid,totalquantity,totalprice)
I want the products bought by all the customers of a particular city 'X'. i.e. every customer in city 'x' should have bought the product. I managed to select the oid's and the pid's of customers living in that particular city. Now I should select the pid's which is present in all the oid's.
Example.
Oid Pid
2400 1
2400 2
2401 3
2401 1
2402 1
2403 1
2403 3
The answer from the above input should be 1 because it is present in all oid's. The query which I used to get the oid's and pid's:
select t.oid,l.pid
from lineitem l
join (select o.oid,c1.cid
from orders o
join (select c.cid
from customer c
where c.city='X') c1
where o.cid=c1.cid) t on l.oid=t.oid
Now I need to intersect all the oid's and get the result.The query should not be dependent on data.
Try:
select pid, count(*)
from (select t.oid, l.pid
from lineitem l
join (select o.oid, c1.cid
from orders o
join (select c.cid from customer c where c.city = 'X') c1
where o.cid = c1.cid) t
on l.oid = t.oid) x
group by pid
having count(*) = (select count(*)
from (select distinct oid
from lineitem l
join (select o.oid, c1.cid
from orders o
join (select c.cid
from customer c
where c.city = 'X') c1
where o.cid = c1.cid) t
on l.oid = t.oid) y) z
I think you can achieve what you want by using IN
I need to write a query that returns the name of the company, and the number of the particular Job Orders that company owns.
Right now my query is like this:
SELECT c.name, cj.joborder_id
FROM company c, joborder jo, candidate_joborder cj
WHERE c.company_id=jo.company_id
AND jo.joborder_id=cj.joborder_id
AND jo.status = 'Active'
AND cj.status=700;
This returns the following table:
Name | Job Order ID
X | 1874
Y | 2003
Y | 2003
Z | 2001
What I want is:
Name | Count
X | 1
Y | 2
Z | 1
Can someone help me with this?
Thanks
The query you want is the following:
SELECT c.name,
count(cj.joborder_id)
FROM company c,
joborder jo,
candidate_joborder cj
WHERE c.company_id=jo.company_id
AND jo.joborder_id=cj.joborder_id
AND jo.status = 'Active'
AND cj.status=700
GROUP BY c.name;
I'd suggest the following references for SQL aggregation and specifically group by and count:
http://www.youtube.com/watch?v=fSH1jpV2nNs
http://www.w3resource.com/sql/aggregate-functions/count-with-group-by.php
use COUNT() and GROUP BY clause,
SELECT c.name, COUNT(cj.joborder_id) TotalCount
FROM company c, joborder jo, candidate_joborder cj
WHERE c.company_id=jo.company_id
AND jo.joborder_id=cj.joborder_id
AND jo.status = 'Active'
AND cj.status=700
GROUP BY c.name
using ANSI JOIN
SELECT c.name,
COUNT(cj.joborder_id) TotalCount
FROM company c
INNER JOIN joborder jo
ON c.company_id = jo.company_id
INNER JOIN candidate_joborder cj
ON jo.joborder_id = cj.joborder_id
WHERE jo.status = 'Active' AND
cj.status=700
GROUP BY c.name
SELECT DISTINCT(c.name), COUNT(cj.joborder_id)
FROM company c, joborder jo, candidate_joborder cj
WHERE c.company_id=jo.company_id
AND jo.joborder_id=cj.joborder_id
AND jo.status = 'Active'
AND cj.status=700
GROUP BY c.name
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."