MySQL condition on joined data - mysql

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.

Related

SQL query how to compare string

There are two tables:
Customers:
ID
Name
Surname
City
Orders:
OrderId
CustomerId
Purchase
Price
I'm trying to find customer id,name,surname where he hasn't Purchase "Pizza".
Any help to fix my query? I tried with cp.Purchase != "Pizza" but doesn't work
SELECT DISTINCT ID,FirstName,LastName
FROM Customers c
INNER JOIN Orders cp ON c.ID = cp.OrderID
ORDER BY c.ID
WHERE cp.Purchase LIKE '%Pizza%'
try this
select * from Customers where Id not in (
select CustomerId From Orders WHERE cp.Purchase LIKE '%Pizza%'
)
The right query is
SELECT DISTINCT c.ID, c.Name, c.Surname
FROM Customers c JOIN Orders o on c.ID = o.CustomerID
WHERE c.ID <> ALL (
SELECT c2.ID
FROM Customers c2 JOIN Orders o2 on c.ID = o2.CustomerID
WHERE o2.Purchase = 'Pizza')
This is because you are looking for who never bought a pizza, so you will select data of customers which ID never appear (<> ALL) in orders table in a record where a pizza was bought.
By the way, check also SQL base, try understand before getting the answer.

How to join + select only customers that have only 1 order?

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
)

Left Join with last insert record from left table and all record form right table

fetch data with join with last insert record from left table based on company id and all record from right table doesn't matter it has any match record in left table.
**company_list**
id
company_id
company_name
company_address1
company_address2
company_phone
company_headoffice
active
**company_subscription_list** table field name is
id
company_id
company_name
company_address1
company_address2
company_phone
company_headoffice
active
and i am using this query for fecth record
SELECT *, c.id AS main_id
FROM company_list as c
LEFT JOIN company_subscription_list as s ON c.company_id = s.company_id
where s.id IN (select max(id) from company_subscription_list GROUP BY company_id)
but its not showing result of those company who didn't subscribe yet.
Need Help.
You have to put you condition in your join condition. Doing so, you will not lose lines :
SELECT *, c.id AS main_id
FROM company_list as c
LEFT JOIN company_subscription_list as s
ON c.company_id = s.company_id
AND s.id IN (select max(id) from company_subscription_list GROUP BY company_id)
Try on this link : http://sqlfiddle.com/#!9/5db87/4

Sql retrieve fields without a relation in another table

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

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."