CREATE VIEW chauffeursreuqired AS
SELECT customers.customer_id, customers.fname, customers.lname, bookings.chauffeur_req, bookings.booking_id
FROM bookings, customers inner join bookings
ON customers.customer_id = bookings.customer_id;
Hey all, so Im trying to create a view using an inner join. im getting the error code as in the title. Anyone has an idea what it means that it is 'ambiguous'?
Thanks.
In this line:
FROM bookings, customers inner join bookings
You are joining bookings to customers (using the implicit join operator ,) and then to bookings again. So you have two bookings table in your JOIN and MySQL can't determine which one to fetch that column from.
You probably only meant to include bookings once, try changing your query to
FROM customers inner join bookings
Or if you do need to join the bookings table twice, you can add an alias to the table names to allow them to be distinguished from each other i.e.
FROM bookings AS bookings_1, customers inner join bookings AS bookings_2
Related
I am trying to make an INNER JOIN statement that will join two tables, the Orders table and the Customer table, these both share the value/key of CustomerID. The Customer table has the information for which state a customer lives in. The Orders table has the information for which customer, according to their customer ID, bought which product. I need to find which products are the top 3 most popular in certain states. Please find the table descriptions images below, so you can understand what I mean.
Orders table:
Customer table:
How can I make this INNER JOIN statement and include the logical operators (and/or) to make this happen?
Thanks!
Try this,
SELECT column_name(s)
FROM Customer
INNER JOIN Order
ON Customer.CustomerID= Order.Customer_ID AND <conditions>;
Inner Join is just only 1 way of joining 2 tables. You can also join these two tables using WHERE closure as follows,
SELECT column_name(s)
FROM Customer c, Order o
WHERE c.CustomerID = o.Customer_ID AND <condition>
TABLE appointment (AppointmentNumber, ApptDate, PatientNumber, DentistID)
TABLE patientstable PatientNumber, PatientForename, PatientSurname, DentistID
TABLE dentists DentistID, DentistForename, DentistSurname
I am running this
SELECT appointment.AppointmentNumber, appointment.ApptDate,
patientstable.PatientForename, patientstable.PatientSurname
FROM patientstable
INNER JOIN appointment ON (patientstable.PatientNumber=appointment.PatientNumber)
Which gives me the dates and patient name details for each appointment.
I want to display the DentistForename and DentistSurname related to each appointment too but I do not know how to do this.
All you need is another join to dentist table like this:
SELECT appointment.AppointmentNumber,
appointment.ApptDate,
patientstable.PatientForename,
patientstable.PatientSurname,
dentist.DentistForename,
dentist.DentistSurname
FROM patientstable
INNER JOIN appointment
ON patientstable.PatientNumber=appointment.PatientNumber
INNER JOIN dentists
ON(appointment.DentistID = dentist.DentistID)
SELECT appointment.AppointmentNumber, appointment.ApptDate, patientstable.PatientForename, patientstable.PatientSurname
FROM patientstable
INNER JOIN appointment
ON patientstable.PatientNumber=appointment.PatientNumber
inner join dentists
on patientstable.dentistsid = dentists.dentistsid
Join One more Tbale to get columns from Dentists Table
I am trying to list two attributes(booking.roomno and room.price) with the condition that "booking.DATETO is not null" from two different tables.
Table: Booking! Table:Room!
I have tried using this command
select booking.roomno,room.price
from booking
inner join room on booking.ROOMNO=room.roomno
where booking.dateto is not null
although the return results came in with duplicated roomno and price like shown below
room.roomno is not unique. It is only unique within a given hotel and your room table contains multiple hotels. You are going to have to specify hotelno in your join condition as well. Also since you might have multiple bookings for the same room (i.e., duplicates in booking table) you will need to do a DISTINCT to prevent that (but then you have to include the hotelno column in your field list):
select DISTINCT booking.roomno,room.price, room.hotelno
from booking
inner join room on booking.ROOMNO=room.roomno
AND booking.hotelno=room.hotelno
where booking.dateto is not null
You have two bookings for the same room so the returned rows match your inner join. You seem to be trying to fetch all the rooms that have bookings. You would achieve that by adding DISTINCTROW before the selected fields.
select DISTINCTROW booking.hotelno, booking.roomno,room.price
from booking
inner join room on booking.ROOMNO=room.roomno AND
booking.HOTELNO=room.HOTELNO
where booking.dateto is not null
I have the (drastically simplified) schema of three tables as follows:
I'm looking to query the database for customers without orders in a certain date range. I know LEFT JOIN can be used to simply find customers with no invoices etc., but I am unsure how I can leverage JOIN (or perhaps even OUTER JOIN) to introduce a date range.
Is this possible using SQL JOIN or should I be looking to use NOT IN syntax to find customers that aren't in a given result set?
My database software is MySQL.
Try this:
select c.*
from customers c left join
invoices i
on c.code = i.customer_code and
i.date between DATE1 and DATE2
where i.customer_code is null;
The key idea is to put the date range in the on clause.
Below is my sql statement
SELECT a.purchase_id,
b.username,
a.purchase_packageid,
a.purchase_tradelimit,
a.purchase_pincode,
a.purchase_datetime,
c.packages_name ,
FROM purchase a,
accounts b,
packages c
WHERE a.purchase_userid=b.UserId
AND c.packages_id=a.purchase_packageid
Basically the issue is I got 3 tables
Accounts
Purchase
Packages
The main table is Purchase, inside the table there is purchase_userid , which I need to use it to get username from table accounts
So the problem now is I got rows where the purchase_userid is blank, because its blank, it won't draw its record as null.
The only record that show in this sql statement is only those with purchase_userid,
As the value for purchase_userid will be fill up later for my web app,
I still want to select rows without purchase_userid and those with purchase_userid
Thanks for helping !!
You need to use a left join to load all records in Purchase even when no matching records are found in Accounts.
SELECT a.purchase_id, b.username, a.purchase_packageid, a.purchase_tradelimit, a.purchase_pincode, a.purchase_datetime, c.packages_name
FROM purchase a LEFT JOIN accounts b
ON a.purchase_userid=b.UserId
JOIN packages c
ON c.packages_id=a.purchase_packageid
This post explains the different kinds of joins pretty well: What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
You're using the old-timey syntax for INNER JOINs between your tables, when what you need is LEFT JOIN operations. Try this:
SELECT a.purchase_id,
b.username,
a.purchase_packageid,
a.purchase_tradelimit,
a.purchase_pincode,
a.purchase_datetime,
c.packages_name
FROM purchase AS a
LEFT JOIN accounts AS b ON a.purchase_userid = b.UserId
LEFT JOIN packages AS c ON a.purchase_packageid = c.packages_id
This works better for you because the kind of JOIN you were using suppresses records from your a table when they weren't matched in your b or c table. This LEFT JOIN will leave the a table records in place, and put NULL values where you are calling for data from the other two.