MySQL join same table twice to fetch value based on different IDs - mysql

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

Related

Select users from table foreach repeated id

I have this table:
users
id | name
---------------------
1 | Lisa
2 | John
and i have this users list:
(1,2,2,2,1,2)
and i want to get this result:
Lisa,John,John,John,Lisa,John
how i can get this result just using SQL query in my Phpmyadmin without using PHP?
i must use Left Join ?
(Assuming orders table is on the same server) You should use inner join here. Because, there should be an user to have an entry in orders table.
SELECT abc = STUFF(( Select user.user_name + ',' from
orders inner join users on users.id = orders.user_id where orders.user_id in (your string of user ids) for
xml_path(' ')), 1, 1, '') FROM temp1

Where clause with multiple OR

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

MYSQLQuery- How to create a query where we have to list out customers who have bought a specific number of album by the same singer

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.

how to get an index and use it in query mysql

I have a table similar to this:
memberID | clubIDs | clubRegistrationDates ...
--------------------------------------------------------------------
2 3,4,10,2 2010,2011,2015,2014 ...
3 2,1,5,6 2015,2000,2005,2010 ...
4 3,2 2014,2014 ...
Meaning of a row is, for example: member 2 was registered to club 3 in 2010, was registered to club 4 in 2011 and so on...
So the query I want to get is to get any member who is a member of club 2 since 2014, so this query should get me members 2 and 4. I already have an sql but it is not connected to year parameter which is:
SELECT clubID, clubName,
(SELECT count(*) FROM members WHERE FIND_IN_SET('2',clubIDs) AND status='1' AND memberTypeID='2') AS activeNumber,
(SELECT count(*) FROM members WHERE FIND_IN_SET('2',clubIDs) AND status='0' AND memberTypeID='2') AS inactiveNumber
FROM clubs;
So I need to get club 2's index from clubIDs column and use it for clubRegistrationDates column. Any help is appreciated, thanks in advance.
Never store multiple values in a single column. This will only get you problems like yours now. A better table design would be
members table
-------------
id
name
...
clubs table
-----------
id
name
...
club_members table
------------------
club_id
member_id
registration_year
It is called a m to n relation. Then to get all members of club with id=2 since 2014 you can do
select m.id, m.name
from members m
join club_members cm on cm.member_id = m.id
where cm.club_id = 2
and cm.registration_year = 2014
And if you only have the club name and not the id then use
select m.id, m.name
from members m
join club_members cm on cm.member_id = m.id
join clubs c on cm.club_id = c.id
where c.name = 'Cool Club'
and cm.registration_year = 2014

How to use dictinct to get single record in many to one relation ship mysql

I have 2 tables like
user user_address
user_id name user_address_id city user_id address_type
1 abc 1 AAA 1 PRESENT
2 AAA 1 PERMANANT
I need query like
select user model1 , user_address model2 where model1.user_id = distinct model2.user_id
Because i know both addresses are same so i need only once.How i can get only one address.
Thanks in advance....
try put "group by" for a field on user_address, if they are the same anyway
select * from user_address inner join user as u on user_id = u.user_id group by city;
If i understood correctly what you need you can try this:
SELECT
*
FROM (
SELECT
user_id,
city ,
address_type
FROM
user_address
GROUP BY
user_id
) as tmp
INNER JOIN user
ON user.user_id = tmp.user_id