Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query with INNER JOIN and WHERE

I am getting the following message when trying to get a specific value in my query

"The specified field could refer to more than one table"

It is pretty clear that I am trying to search something present in more than one table, but how to do this right?

now I have the following code:

SELECT Table1.CustomerId, Table1.Address, 
       Table2.CustomerId, Table2.Telephone, 
       Table3.CustomerId, Table3.Notes
FROM (Table1 INNER JOIN Table2 ON Table1.CustomerId=TAble2.CustomerId) 
      INNER JOIN Table3 ON Table2.CustomerId=Table3.CustomerId
WHERE CustomerId = 0015

the last sentence is the problem... any ideas?

like image 265
Felipe Avatar asked Feb 03 '26 18:02

Felipe


1 Answers

The error message is pretty clear, the field CustomerId in the WHERE clause WHERE CustomerId = 0015, is presented in both the two tables. You have to determine from which table you want to use it from; table1 or table2? for example:

SELECT Table1.CustomerId, Table1.Address, 
       Table2.CustomerId, Table2.Telephone, 
       Table3.CustomerId, Table3.Notes
FROM (Table1 INNER JOIN Table2 ON Table1.CustomerId=TAble2.CustomerId) 
      INNER JOIN Table3 ON Table2.CustomerId=Table3.CustomerId
WHERE table1.CustomerId = 0015
like image 65
Mahmoud Gamal Avatar answered Feb 06 '26 08:02

Mahmoud Gamal