Learning Microsoft Access and SQL I've normalized several fields into four (4) tables.
Why am I not able to run the SQL query below in MS Access?
SELECT CUST_FNAME, CUST_LNAME
FROM RENTALS, CUSTOMERS
WHERE ID = CUST_ID AND MEDIA_TITLE = "The Love Boat";
I get the following error:
Learning Microsoft Access and SQL I've normalized several fields into four (4) tables.
Why am I not able to run the SQL query below in MS Access?
SELECT CUST_FNAME, CUST_LNAME
FROM RENTALS, CUSTOMERS
WHERE ID = CUST_ID AND MEDIA_TITLE = "The Love Boat";
I get the following error:
Share Improve this question edited Jan 21 at 22:21 philipxy 15.1k6 gold badges42 silver badges95 bronze badges asked Jan 20 at 19:11 blogger13blogger13 977 bronze badges 5 |1 Answer
Reset to default 4There is no ID
column. You have two tables that both have CUST_ID
columns, and therefore to avoid ambiguity you must prefix the column name with the table name.
Even better, when you're in that situation you should give your tables a mnemonic alias and for the love of God don't use that janky ancient FROM A,B
syntax for JOINs.
SELECT c.CUST_FNAME, c.CUST_LNAME
FROM RENTALS r
INNER JOIN CUSTOMERS c ON c.CUST_ID = r.CUST_ID
WHERE r.MEDIA_TITLE = 'The Love Boat';
The other thing I'd do is add an id column to the Media
table, since there can certainly be a number of items sharing the same title
JOIN
syntax in the ANSI-92 SQL Standard (more than 30 years ago!) and its use is discouraged – marc_s Commented Jan 20 at 19:14FROM Customer c INNER JOIN Rentals R ON
and then start using those aliases to define which column (from which table) you want – marc_s Commented Jan 20 at 19:15SELECT CUST_FNAME, CUST_LNAME FROM RENTALS R INNER JOIN CUSTOMERS C ON R.CUST_ID = C.CUST_ID WHERE MEDIA_TITLE = "The Love Boat";
Assuming you want all customers who have rented "the Love Boat" and not just all customers and call out those who have rented the movie in question. If you used the graphical query builder in Access, it wouldn't use the comma in the from – xQbert Commented Jan 20 at 19:17