The service_option table and the room table have a same column room_key, the room table and the user table share a same column user_key, what I want is obtaining user's infomation from service options.
SELECT user_key, user_id, user_status, user_company_name
FROM user
WHERE user_key
IN (
SELECT user_key
FROM room
WHERE room_key
IN (
SELECT room_key
FROM service_option
WHERE service_option_key = 3 AND ordered_service_option_status !=0
)
)
Using IN nested in another IN turned to be very inefficient.
This is probably the query you're looking for:
SELECT U.user_key
,U.user_id
,U.user_status
,U.user_company_name
FROM [user] U
INNER JOIN room R ON R.user_key = U.user_key
INNER JOIN service_option S ON S.room_key = R.room_key
AND S.service_option_key = 3
AND S.ordered_service_Option_status != 0
The INNER JOIN approach will be much more performant and make the query more readable.
Hope this will help you.
Do not use WHERE IN, but join :
SELECT u.user_key, u.user_id, u.user_status, u.user_company_name
FROM user u, room r, service_option so
WHERE u.user_key = r.user_key
AND r.room_key = so.room_key
AND so.service_option_key = 3
AND so.ordered_service_option_status != 0
SELECT u.user_key, u.user_id, u.user_status, u.user_company_name
FROM
user u join room r on u.user_key = r.user_key
join service_option so on so.room_key = r.room_key
WHERE
so.service_option_key = 3 AND so.ordered_service_option_status !=0
Related
I try to improve this query but I do not made successefully. I use a some left join and subquerys (I don't know another form) We try to get all bookings from users with certain status and the number of bookings multiples related with this booking and get from the log user the origin. The query is slow even if use a limit clausure. I Appreciate all the help can you give me.
This is the query:
SELECT DISTINCT b.uneaque_id, b.id, b.status, b.route_status, b.username, b.purpose, b.transfer, b.pickup_date, b.pickup_time, b.return_time, b.amount, b.default_location, b.start_address_route_comments, b.start_address_route, b.end_address_route_comments, b.end_address_route, u1.first_name, u1.last_name, b.transaction_Id, b.manual_payment, mr.AddressCount, lu.origin
FROM bookingdetails b
LEFT JOIN log_users lu ON lu.uneaque_id = b.uneaque_id AND lu.command_type = 'NEW BOOKING'
LEFT JOIN (
SELECT booking_id, COUNT(*) AS AddressCount FROM booking_multiple_rides GROUP BY booking_id
) mr ON b.id = mr.booking_id,
userdetails u1 WHERE b.username = u1.email
AND u1.user_status IN ('Active', 'Blocked', 'Not_Active')
AND b.default_location = 1
PD: Sorry for my english.
You have a ON b.id = mr.booking_id, userdetails u1 WHERE
you should change with a proper inner join
SELECT DISTINCT b.uneaque_id
, b.id, b.status
, b.route_status
, b.username
, b.purpose
, b.transfer
, b.pickup_date
, b.pickup_time
, b.return_time
, b.amount
, b.default_location
, b.start_address_route_comments
, b.start_address_route
, b.end_address_route_comments
, b.end_address_route
, u1.first_name
, u1.last_name
, b.transaction_Id
, b.manual_payment
, mr.AddressCount
, lu.origin
FROM bookingdetails b
LEFT JOIN log_users lu ON lu.uneaque_id = b.uneaque_id AND lu.command_type = 'NEW BOOKING'
LEFT JOIN (
SELECT booking_id
, COUNT(*) AS AddressCount
FROM booking_multiple_rides GROUP BY booking_id
) mr ON b.id = mr.booking_id
INNER JOIN userdetails u1 ON b.username = u1.email
AND u1.user_status IN ('Active', 'Blocked', 'Not_Active')
AND b.default_location = 1
and be sure you have proper index on
table bookingdetails a composite index on columns ( uneaque_id , id, default_location)
table log_users a composite index on columns (uneaque_id, command_type)
table userdetails a cmposite index on columns (email,user_status )
Tip 1.
Hiding a derived table in a LEFT JOIN is a prescription for sluggishness.
Replace
mr.AddressCount
with
( SELECT COUNT(*)
FROM booking_multiple_rides
WHERE booking_id = b.id
GROUP BY booking_id ) AS AddressCount
and get rid of the LEFT JOIN ( SELECT ... ) AS mr ON ..
Tip 2 Use explicit JOINs, no the old-fashioned "comma-join":
JOiN userdetails u1
ON b.username = u1.email
This won't help performance but it will make things clearer.
Tip 3: If you need an INNER JOIN (u1) after a LEFT JOIN, use parentheses. Else, put the inner joins first, then the left joins. This makes it easier to use, but may screw up the logic.
Tip 4: Don't use LEFT unless you need it. When you dont need it, it confuses the reader (and the Optimizer). (Again, no performance change.)
Tip 5: Why are you using DISTINCT? That takes an extra pass over all the resultset.
If those do not help enough, then provide SHOW CREATE TABLE so we can critique the indexes.
I have a mysql select statement as below:-
select user.username, chat.from_user,chat.to_user,chat.message,chat.date_created
from chat join user on (chat.from_user or chat.to_user) in (user.user_id)
where(chat.from_user = 3 or chat.to_user = 3) and chat.chat_id IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
and here is my result
I always get username = admin. My expectation result is username will get correct from / to user.
Please help. Thank you.
SELECT
IF(chat.from_user=3, to_user
, IF(chat.to_user=3, form_user, 0)) AS username,
chat.from_user,chat.to_user,chat.message,chat.date_created
FROM chat
LEFT JOIN user fr_user ON chat.from_user = user.user_id
LEFT JOIN user to_user ON chat.to_user = user.user_id -- since you only want to show the TO_USERNAME, you can remove above line
WHERE (chat.from_user = 3 OR chat.to_user = 3) and chat.chat_id
IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
I think you intend:
select u.username, c.from_user, c.to_user, chat.message, c.date_created
from chat c join
user u
on u.user_id in (c.from_user, c.to_user)
where 3 in (c.from_user, c.to_user) and
c.chat_id in (select max(c2.chat_id)
from chat c2
group by chat_group_id
);
I want to make a query to select all of the workshop information that the user isn't registered for. I want to have all the data with 1 query.
This is the : workshops table
The : registrations table
The : workshop_rounds table
Hopefully some of you guys could help me.
Thanks in advance!
I've tried this query, but it is not what i want, it is close but not fully.
SELECT * FROM `workshop_rounds` WHERE `id` != ALL (SELECT `workshop_round_id` FROM `registrations` WHERE `user_id` = 1) AND `workshop_id` != ALL (SELECT `workshop_id` FROM `workshop_rounds` WHERE `workshop_id`= ALL (SELECT `workshop_round_id` FROM `registrations` WHERE `user_id` = 1))
The result is:
But now i also want to not get the other rounds with the same workshop_id as the round the user is registered for. Hopefully you understand me.
Using NOT EXISTS
SELECT *
FROM workshops w
WHERE NOT EXISTS
(
SELECT 1
FROM registrations r
JOIN "workshop_rounds" wr ON r."workshop_round_id" = wr.id
WHERE wr.workshop_id = w.id
AND r."user_id" = 1
);
and maybe more concise NOT IN
SELECT *
FROM workshops w
WHERE w.id NOT IN
(
SELECT wr.workshop_id
FROM registrations r
JOIN "workshop_rounds" wr ON r."workshop_round_id" = wr.id
WHERE r."user_id" = 1
);
For a single user, I would approach this with not exists:
select w.*
from workshops w
where not exists (select 1
from registrations r
where r.workshop_id = w.id and
r.user_id = THE USER ID
);
Try this one to fetch all workshops that user not registered
select ws.* from workshops ws
where ws.id not in (select w.id from workshops w, workshop_rounds wr, registrations r
where r.workshop_round_id = wr.id and wr.workshop_id=w.id and r.user_id = THE_USER_ID);
I'm trying to combine the results of two queries. I'm not very proficient in mysql so I'm here for some help.
The first query is as follows:
select count(roomtypeid) as bookedrooms, day
from event_guest_hotel
where hotelid = 1 and roomtypeid = 1
group by day;
This returns:
The second query:
SELECT ehr.reservationid, ehr.day, h.name AS hotelname,
ehr.totalrooms as requested_rooms, r.name AS roomname
FROM event_hotel_reservation ehr
INNER JOIN hotel_room_type r
ON ehr.roomtypeid = r.roomtypeid
INNER JOIN hotel h
ON ehr.hotelid = h.hotelid
WHERE totalRooms != 0
AND reservationID = '1'
This returns:
Can I combine the first query with the second one, so I get the results of the first one in another resultcolumn next to 'roomname'? That way I know how many rooms are already booked and how many were originally requested from one single query.
Try:
SELECT ehr.reservationid, ehr.day, h.name AS hotelname,
ehr.totalrooms as requested_rooms, r.name AS roomname,
egh.bookedrooms
FROM event_hotel_reservation ehr
INNER JOIN hotel_room_type r ON ehr.roomtypeid = r.roomtypeid
INNER JOIN hotel h ON ehr.hotelid = h.hotelid
left outer join (
select hotelid, count(roomtypeid) as bookedrooms, day
from event_guest_hotel
where roomtypeid = 1
group by hotelid, day
) egh on h.hotelid = egh.hotelid and ehr.day = egh.day
WHERE totalRooms != 0
AND reservationID = '1'
I have three tables that have common affiliate_id field:
impressions, clicks, commissions
I want to select all the data from these three tables where the affiliate_id = '$iAid' in one query, then handle the data in php.
How would I write a query like this?
Here's PHP code assuming you're using MYSQL:
$qry = "SELECT im.*, cl.*, co.*
FROM `impressions` im, `clicks` cl, `comissions` co
WHERE im.`affiliate_id`=cl.`affiliate_id`
AND cl.`affiliate_id`=co.`affiliate_id`
AND im.`affiliate_id`='".$iAid."'";
SELECT <YOUR COLUMN LIST>
FROM impressions a, clicks b, commissions c
WHERE a.affiliate_id = '$iAID'
AND a.affiliate_id = b.affiliate_id
AND b.affiliate_id = c.affiliate_id
SELECT * FROM impressions AS i JOIN clicks AS c ON i.affiliate_id = c.affiliate_id JOIN commissions AS m ON i.affiliate_id = m.affiliate_id WHERE i.affiliate_id = '$iAid'
should do the trick
Do a left join in mysql query:
Select * from Impressions imp
Left join clicks c on c.affiliate_id = imp.affiliate_id
Left join commissions com on com.affiliate_id = imp_affiliate_id
Where ***
limit ***
ect....
select * from
impression a
join clicks b on (a.affiliate_id = b.affiliate_id)
join commissions c on (a.affiliate_id = c.affiliate_id)
where a.affiliate_id = ?
How about a join:
select * from impressions, clicks, commissions
where impressions.affiliate_id = clicks.affiliate_id
and clicks.affiliate_id = commissions.affiliate_id
and impressions.affiliate_id = '$iAid'