i cant get around this question in my assesment,need a little help,
q)What are the BookingIDs and check in and out dates for bookings for Michelle Bonnier?
using the global holidays database.
A)
select bookingid,guestid,firstname,lastname,checkindate,checkoutdate
from guests
join bookings on guests.GuestID = bookings.GuestID
where firstname = "Michelle"
and lastname = "Bonnier";
Error Code: 1066. Not unique table/alias: 'guests'
As guestid is in both tables in the select column list we need to say that which one to use, So try this:
SELECT bookingid,
guests.guestid,
firstname,
lastname,
checkindate,
checkoutdate
FROM guests
JOIN bookings
ON guests.guestid = bookings.guestid
WHERE firstname = "michelle"
AND lastname = "bonnier"
Always try to use table name alias
select
b.bookingid,g.guestid,g.firstname,g.lastname,
b.checkindate,b.checkoutdate
from guests g
join bookings b on g.GuestID = b.GuestID
where g.firstname = "Michelle"
and g.lastname = "Bonnier";
Related
I have three tables
user_table:
user_id
user_name
leave_type:
leave_id
leave_name
leave_taken
id
leave_id
applied_by (user_id)
approved_by (user_id)
My end result should be
Result:
leave_name
applied_by (user_name)
approved_by (user_name)
This is what I have tried and got stuck at. I'm sorry for not providing what I tried the first time I posted this question.
Option 1:
SELECT leave_type.leave_name, users.user_name as applied_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
from leaves
join leave_type on leaves.leave_type = leave_type.id
join users on leaves.applied_by = users.id
Option 2:
SELECT leave_type.leave_name, users.user_name as applied_by,
leaves.approved_by, leaves.no_of_days, leaves.leave_date,
leaves.leave_upto_date, leaves.leave_status, leaves.approved_on
FROM leave_type, leaves, users
WHERE leave_type.id = leaves.leave_type
AND leaves.applied_by = users.id
P.S. I'm new to MySQL & I'm not sure how to implement this.
I achieved your desired result with the following query. I'm not very sure if this is the best way though.
SELECT leave_type.leave_name, users.user_name AS applied_by,
(SELECT user_name FROM users WHERE id = leaves.approved_by) AS approved_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
FROM leaves
JOIN leave_type on leaves.leave_type = leave_type.id
JOIN users on leaves.applied_by = users.id;
If you need to join the same table twice, you can provide it with an alias.
In this case, the user table has been joined with two different aliases. I've made the aliases in capitals, so it's easier to find them, but of course, you can write them however you want.
SELECT
leave_type.leave_name,
APPLY_USER.user_name as applied_by,
APPROVE_USER.user_name as approved_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
from leaves
join leave_type on leaves.leave_type = leave_type.id
join users AS APPLY_USER on leaves.applied_by = APPLY_USER.id
join users AS APPROVE_USER on leaves.applied_by = APPROVE_USER.id
i have the following tables:
Technician
Tech_ID,First_Name,Last_Name
RT_QUEUE_Delta
Tech_ID, RT_Complete` (references a `Tech_ID` in `Technician`).
I need to get the data from a row in RT_Queue_Delta where RT_Completed = ?? but in my output I need to have the First_Name and Last_name that correlates with Tech_id and RT_Completed.
I can match one but I don't know how to match both. I tried:
select RTTech.First_Name as RT_First_Name,
RTTech.Last_Name as RT_Last_Name
from Technician as RTTech
Join RT_Queue_Delta as RT
on RT.RT_Completed = RTTech.Tech_ID
You can join to the Technician table multiple times:
select d.tech_id, t.first_name, t.last_name,
d.rt_completed as completed_id,
t2.first_name as completed_first_name,
t2.last_name as completed_last_name
from RT_QUEUE_Delta d
join Technician t on d.tech_id = t.tech_id
join Technician t2 on d.RT_Completed = t2.tech_id
I need to create a single query that reads from three tables in the same db.
Tables:
1. cm_players
player_id
card_no
club_id
surname
name
birthdate
2. cm_clubs
club_id
prefix
name
address
place
stadium_id
telephone
fax
email
web
contact_person
president
secretary
press_officer
status
image
3. cm_registrations
player_id
surname
name
birthdate
birthplace
club_id
date_from
date_to
amateur
status
So, I need to display
cm_players.name,
cm_players.surname,
cm_clubs.name,
cm_players.birthdate,
cm_players.card_no,
join cm_players with cm_clubs to get the name of the club, and check in cm_registrations if the user is active.....
I have tried something like:
SELECT cm_players.name, cm_players.surname, cm_clubs.name, cm_players.birthdate, cm_players.card_no, cm_registrations.amateur FROM cm_players
INNER JOIN cm_clubs on cm_players.club_id = cm_clubs.club_id, cm_registrations
WHERE cm_players.name LIKE '%$name%' AND cm_players.surname LIKE '%$surname%' AND cm_players.player_id = cm_registrations.player_id AND cm_registrations.amateur = 0 ORDER BY cm_players.player_id ASC
but no luck...
can anyone help me to build this query?
Here we are setting the table aliases, and get the column values by using appropriate alias names of that table and we are using inner joins on two tables that are clubs and registrations
SELECT cmp.name, cmp.surname, cmc.name, cmp.birthdate, cmp.card_no, cmr.amateur
FROM cm_players cmp
INNER JOIN cm_clubs cmc on cmp.club_id = cmc.club_id
INNER JOIN cm_registrations cmr on cmp.player_id = cmr.player_id
WHERE cmp.name LIKE '%$name%'
AND cmp.surname LIKE '%$surname%'
AND cmr.amateur = 0
ORDER BY cmp.player_id ASC
cm_registrations WHERE cm_players.name LIKE '%$name%' AND cm_players.surname LIKE '%$surname%' AND cm_players.player_id = cm_registrations.player_id AND cm_registrations.amateur = 0 ORDER BY cm_players.player_id ASC
error is after join u use , than cm_registeration change it and try to fix it.
I have two tables..
Persons:
empid(primary key)
firstname
lastname
email
Details:
Did(primary key)
salary
designation
empid
Now I need to UPDATE email of the employee whose name is 'abc' AND designation is Manager.(lets suppose there are more than one employees names abc and therefore designation needs to be checked)
I am using sql server 2008
UPDATE p
SET email = 'newemail#wherever.com'
FROM dbo.Persons AS p
INNER JOIN dbo.Details AS d
ON p.empid = d.empid
WHERE p.firstname = 'abc'
AND d.Designation = 'manager';
Try This:
UPDATE
[Persons]
SET
[Persons].[email]='#######.###'
FROM
[Persons]
INNER JOIN [Persons].[empid]
ON [Details].[empid] = [Persons].[empid]
WHERE
[Persons].[firstname]='abc' AND
[Details].[designation]='Manager'
Hey guys I am trying to sum these tables and I can't figure out what I'm doing wrong
My first table is:
Reserve
ReserveID MembershipID PlayerCount Time CourseID
My second table is
Courses
CourseID Name
My sql statement that I am trying is here:
Select Sum(Reserve.Player_Count)Total
From Reserve
Left Join Courses
On Courses.CourseID = Reserve.ReserveID
Where Time = "2012-04-09 07:10:00"
And Courses.Name = "Lake" (or I had Courses.CourseId = "1")
You seem to mixing alias. Avoid them
Select Sum(Reserve.Player_Count)Total
From Reserve
Left Join Courses
On Courses.CourseID = Reserve.ReserveID
Where Reserve.Time = "2012-04-09 07:10:00"
-- ^ Right here
And Courses.Name = "Lake" (or I had Courses.CourseId = "1")