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."
Related
CUSTOMERS
NAME
ID
ORDERS
CID
ITEMS
New customer orders in orders table have new data record like:
CID 134 - CAR
CID 135 - PHONE
CID 134 - TEA
I need to select only customers that have 1 record in the orders table, in dat above its CID 135
IDN=CID
I need to select IDN that have only one ITEMS record, I tried:
SELECT customers.name, orders.items
FROM customers JOIN orders
WHERE Items > 2
but doesn't work :(
You just need to modify your where clause. This query will return only customers that have exactly one row in the orders table.
SELECT customers.name,
orders.items
FROM customers
LEFT JOIN orders
on customers.ID = orders.CID
WHERE customers.ID IN (SELECT CID from orders GROUP BY CID HAVING COUNT(*) = 1)
Try adding an ON statement for your join. This tells SQL how to connect the two tables.
SELECT
customers.name
, orders.items
FROM customers
JOIN orders
on customers.ID = orders.CID
WHERE Items > 2
You must add a another subquery which count the numbers,
CREATE tABLE customers (ID int, name varchar(10))
INSERT INTO customers VALUES (1,'A'),(2,'b')
CREATE tABLE orders (ID int,CID int,items varchar(10))
INSERT INTO orders VALUES (1,1,'car'),(2,1,'car2'),(3,2,'car'),(4,2,'car2'),(5,2,'car2')
SELECT
customers.name
, orders.items
FROM customers
JOIN orders
on customers.ID = orders.CID
INNER JOIN (SELECT COUNT(*) countr,CID FROM orders GROUP BY CID) o1 on o1.CID = orders.CID
WHERE countr > 2
name | items
:--- | :----
b | car
b | car2
b | car2
db<>fiddle here
You can do:
select * from customers
where id in (
select cid from orders group by cid having count(*) = 1
)
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)
I have two tables "customers" and "campaigns":
customers
-------------
id | int
-------------
name | varchar
campaigns
----------------------
id | int
----------------------
customers_id | int
----------------------
name | varchar
A customer can have multiple campaigns. The association between the two tables is via campaigns.customers_id = customers.id.
Now I want to get all customers that have gotten campaign with name "A" and name "B".
I tried a JOIN with an IN statement, but it returns all customers, that have received any of campaign "A" or "B":
SELECT
customers.name
FROM
customers
JOIN
campaigns
ON
customers.id=campaigns.customers_id
WHERE
campaigns.name IN('A','B')
Thanks a lot!
You have joined campaigns table with customers.id=campaigns.id instead of customers.id=campaigns.customers_id.
Correct Query is
SELECT
customers.name
FROM
customers
JOIN
campaigns
ON
customers.id=campaigns.customers_id
WHERE
campaigns.name IN('A','B')
This should work :)
You'll need to join for each campaign
SELECT
customers.name
FROM
customers
INNER JOIN campaigns c1 ON customers.id=c1.id and c1.name = 'A'
INNER JOIN campaigns c2 ON customers.id=c2.id and c2.name = 'B'
This will now contain customers who have received both.
SELECT
customers.name
FROM
customers
LEFT JOIN campaigns c1 ON customers.id=c1.customers_id and c1.name = 'A'
LEFT JOIN campaigns c2 ON customers.id=c2.customers_id and c2.name = 'B'
With the first LEFT JOIN you will get the customers that have campaigns and the campaign names are 'A', and then the result will join with the campaigns whose have name 'B'.
Only the rows with campaign.name = A and campaign.name = B will be selected.
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;
I have 2 tables: Customers and Orders. A customer has many orders and an order belongs to a customer. The order can be approved (marked by a field approved_at).
I want to retrieve all the customers without any approved order. This includes customers without any order and customers with orders that aren't approved (approved_at = null).
Can I do this in a single query without subqueries?
SELECT ..., COUNT(Orders.id) AS cnt
FROM Customers
LEFT JOIN Orders ON (Customers.id = Orders.Customer_id) AND (Orders.approved_at is null)
HAVING cnt = 0
SELECT c.*
FROM Customers c
LEFT JOIN Orders o ON c.ID = o.CustomerID
WHERE o.ID IS NULL OR c.Approved_at IS NULL