I was trying to access list of foods with which has restaurant_id = 2 and if the food_id in carts table it should print cart_id and if the food_id in favorites table it should print favorite_id otherwise it will return null. But the problem i'm facing here is food_id i'm getting from that specific restaurant but while checking the carts and favorites id it fails to check for the user_id.
food_id is in the carts table but not for that user but it shows that cart_id. How do i make the query correct?
Foods table: id, restaurant_id
Carts Table: id, user_id, food_id
Favorites Table: id, user_id, food_id
SQL QUERY:
SELECT foods.id, carts.id as cart_id , favorites.id as favorite_id
FROM foods
LEFT JOIN carts
ON carts.food_id= foods.id
LEFT JOIN favorites
ON favorites.food_id= foods.id
WHERE foods.restaurant_id = 2
AND (carts.user_id = 1 OR favorites.user_id = 1)
you need to use something like this:
SELECT foods.id, carts.id as cart_id, favorites.id as favorite_id
FROM foods
left join carts on carts.food_id=foods.id and carts.user_id = 1
LEFT JOIN favorites on favorites.food_id=foods.id and favorites.user_id = 1
WHERE foods.restaurant_id = 2
note that you need to filter both tables (cards/favorites) by user id
Related
I have three tables with the following structures:
users Table
id
first_name
last_name
company
...
5
John
Doe
Company Name
...
6
Joe
Bloggs
Bloggs Inc.
...
...
...
...
...
...
vehicles Table
id
name
user_id
...
1
2020 VW Tiguan
5
...
...
...
...
...
bids Table
id
bidder_user_id
vehicle_id
amount
...
1
6
1
4000
....
...
...
...
...
...
I am trying to get all bids along with the ID of the vehicle being bid on, the name of the user_id who owns the vehicle, the name of the bidder_user_id who bid on the vehicle, and the bid amount.
My problem is that it seems I need to conduct two JOIN statements on the users table in order to match the bidder_user_id from bids, and vehicle.user_id from vehicles however that doesn't seem to be possible.
Here is my query so far:
SELECT
vehicle_id AS vehicleID,
vehicles.user_id AS sellerID,
bids.bidder_user_id AS bidderID,
CONCAT(users.first_name, ' ', users.last_name) AS bidderName,
users.company AS bidderCompanyName,
bids.amount AS bid_amount,
FROM `bids`
JOIN `users` ON bids.bidder_user_id = users.id
JOIN `vehicles` ON bids.vehicle_id = vehicle.id
ORDER BY vehicle_id DESC;
Just join the users table twice:
SELECT bids.id,
bids.amount,
CONCAT(owners.first_name, ' ', owners.last_name) as owner,
CONCAT(bidders.first_name, ' ', bidders.last_name) as bidder
FROM bids
JOIN users as bidders
ON bids.bidder_user_id = bidders.id
JOIN vehicles
ON bids.vehicle_id = vehicles.id
JOIN users as owners
ON vehicles.user_id = owners.id
So I have this two table where it records what kind of food is the user's favorite:
users table
------------
id | country
------------
1 | US
2 | PH
3 | US
4 | US
5 | PH
food_favourites table
-----------------
food_id | user_id
-----------------
3 | 1
7 | 1
3 | 2
3 | 3
3 | 4
I want to know how many unique users from US tagged food_id 3 as their favorite.
So far I have this query:
select *, count(user_id) as total
from food_favourite
inner join users on users.id = food_favourites.user_id
where food_favourites.food_id = 3
and users.country = 'US'
group by users.id
Well This doesn't work coz it returns total to 4 instead of just 3.
I also tried doing subqueries - no luck, I think I'm missing something.
DROP TABLE IF EXISTS users;
CREATE TABLE users
(user_id SERIAL PRIMARY KEY
,country CHAR(2) NOT NULL
);
INSERT INTO users VALUES
(1,'US'),
(2,'EU'),
(3,'US'),
(4,'US'),
(5,'EU');
DROP TABLE IF EXISTS favourite_foods;
CREATE TABLE favourite_foods
(food_id INT NOT NULL
,user_id INT NOT NULL
,PRIMARY KEY(food_id,user_id)
);
INSERT INTO favourite_foods VALUES
(3,1),
(7,1),
(3,2),
(3,3),
(3,4);
SELECT COUNT(DISTINCT u.user_id) distinct_users
FROM users u
JOIN favourite_foods f
ON f.user_id = u.user_id
WHERE u.country = 'US'
AND f.food_id = 3;
+----------------+
| distinct_users |
+----------------+
| 3 |
+----------------+
First of all the answer to the above question should be 3 as id 1,3,4 all have food_id 3 as their favorite food.
To just print the query try this, it will surely work:
select count(*) as total from food_favourites
inner join users on users.id=food_favourites.user_id
where food_id=3 and country='US';
I want to know how many unique users from US tagged food_id 3 as their favorite.
You count unique values with COUNT DISTINCT:
select count(distinct ff.user_id) as total
from food_favourite ff
inner join users u on u.id = ff.user_id
where ff.food_id = 3
and u.country = 'US';
Don't group by user, because you don't want a result per user. You want one row with one number, telling you how many US users prefer food 3.
An alternative that I prefer over the join. The query reads like I would word the task: count users from US that like food 3.
select count(*) as total
from users
where country = 'US'
and id in (select user_id from food_favourites where food_id = 3);
No unnecessary join and hence no need to get back to distinct values.
The sub_query is
SELECT u.country, f.food_id, COUNT(u.id) AS 'Total users'
FROM users u
INNER JOIN food_favourites AS f ON (u.id = f.[user_id])
WHERE u.country = 'US'
GROUP BY u.country, f.food_id
select count(user_id) as total, Country
from food_favourites
inner join users on users.id = food_favourites.user_id
where food_favourites.food_id = 3
and users.country = 'US'
group by country
Untested, but I think this is what you're after? This will return results only for the US and a food id of 3. If you want something more reusable that you can simply loop through the results for ALL countries...something like this should work (once again, untested...):
select count(user_id) as total, Country, food_id
from food_favourites
inner join users on users.id = food_favourites.user_id
group by country, food_id
order by country, food_id
Try:
select count(user_id) as total
from food_favourites
inner join users on users.id = food_favourites.user_id
where food_favourites.food_id = 3
and users.country = 'US'
We have the following tables:
Customer:
customer_id (pk)
customer_name
customer_dob
adress
gender
Album:
Album_id (PK)
album_name
singer_id (FK)
Order_Details:
order_id (Pk)
quantity purchased
customer_id (fk)
Order_Basket:
Order_id (fk)
album_id (fk)
I want to create an SQL query statement where I want to list out customers that have purchased 4 or more albums by the same singer. I'm very new to this and I would like to seek input from the much more professional individuals here.
try this:
select customer.customer_id, album.singer_id , count(*)
from customer ,order_details, order_basket, album
where customer.customer_id = order_details.customer_id
and order_details.order_id = order_basket.order_id
and order_basket.album_id = album.Album_id
group by customer.customer_id, album.singer_id
having count(*) > 3
WITH PurchaseSummary AS (
SELECT d.customer_id, a.singer_id, count(a.album_id) AS album_count
FROM Order_Details d
LEFT JOIN Order_Basket b ON on b.Order_id = d.Order_id
LEFT JOIN Album a ON a.Album_id = b.album_id
GROUP BY d.customer_id, a.singer_id)
SELECT ps.customer_id, ps.singer_id, ps.album_count
FROM PurchaseSummary ps
LEFT JOIN Customer c ON c.customer_id = ps.customer_id
WHERE ps.album_count >= 4
ORDER BY d.customer_id
Use a subquery to look at all of your orders. Because an order may have more than one album, tack on the order_basket table. Now you have all orders with albums. Tack on the Album table and you have the artist name for each of the orders. Because you also have the customer ID, you can group the customer ID and singer ID with a count of the albums. The result will be like:
Customer|Artist|Num Albums
1 A 1
1 B 4
2 A 1
2 B 6
2 C 5
Then, you select your customer ID and artist ID from that table with your condition they have to have bought >=4 albums from that artist. Lastly, join the Customer table on to get further details about that customer. If you have an Artist table too, you can join that on the Artist ID to get things like name, etc.
I have 2 tables in a database person and order tables.
PERSON table:
PERSON_ID | NAME
ORDER table:
ORDER_ID | ORDER_NO | PERSON_ID
I need to display all the orders + a name of corresponding person if it exists, if not just order details.
So far I got up to query:
SELECT ORDER_ID, ORDER_NO, order.PERSON_ID, NAME
FROM person, order
WHERE person.PERSON_ID = order.PERSON_ID AND
person.FIRST_NAME IS NOT NULL;
Which gives me orders only if the name is available whereas I need to display all the orders despite the fact if name is available or not.
Any suggestions?
Yes, you can use LEFT JOIN for that:
SELECT o.order_id, o.order_no, o.person_id, p.name
FROM `order` o
LEFT JOIN person p
ON p.person_id = o.person_id AND p.FIRST_NAME IS NOT NULL
With LEFT JOIN if the name is null it will still give you the orders.
I have four tables that contain some fields
1. user(id, name, email, password, .....)
2. policy1(id, userid, ....)
3. policy2(id, userid, ....)
4. policy3(id, userid, ....)
Now this is clear that user's primary key (id) is foreign key in other three tables.
I want to fetch total number of tables against each user that contain user id in that table.
for example:
user id 1 has entry in any two tables,
user id 2 has entry in three tables,
user id 3 has entry in any single table
Result should be like
id total
1 2
2 3
3 1
I tried using Left Join but that I could not get the expected result.
You can use a subquery to get the count for each user and then add the values together to get the final result. I am using a LEFT JOIN to return all user rows even if there is not a matching value in the other tables. If you only want the users with values in the other tables, then you can use an INNER JOIN:
select u.id,
Coalesce(CntP1, 0) + Coalesce(CntP2, 0) + Coalesce(CntP3, 0) TotalCount
from `user` u
left join
(
select count(*) CntP1, userid
from policy1
group by userid
) p1
on u.id = p1.userid
left join
(
select count(*) CntP2, userid
from policy2
group by userid
) p2
on u.id = p2.userid
left join
(
select count(*) CntP3, userid
from policy3
group by userid
) p3
on u.id = p3.userid