Avoiding an "Undefined field in a query" without adding a table name or table alias in the where section

I have a select query in which I joined several tables: T1and T2, and both tables have a STATUS field that I do not need to retrieve. In the where clause, I need to add WHERE STATUS=1some more conditions.

But for some reason, I just can’t add the table name or table alias to the field in the where ie clause I cannot use where T2.STATUS=1. Is there a way to always read STATUS=1from the where clause T1.STATUSso that I can avoid the "Ambiguous Field Error"?

Here is an example request:

select T1.name, T1.address, T1.phone, T2.title, T2.description from T1
Left Join T2 on T1.CID=T2.ID
where STATUS = 1

In the previous request, I want to STATUS =1always meanT2.STATUS

+5
source share
1 answer

If for some reason you cannot live with fulfillment

select T1.name, T1.address, T1.phone, T2.title, T2.description from T1
Left Join T2 on T1.CID=T2.ID
where T2.STATUS = 1

Then I think you could

SELECT T1.name, T1.address, T1.phone, T2.title, T2.description 
FROM (  SELECT CID, name, address, phone
        FROM T1) AS T1
LEFT JOIN T2
ON T1.CID=T2.ID
WHERE STATUS = 1

Basically just skip getting the STATUS column from T1. Then there can be no conflict.

Bottomline; There is no easy way to do this. Closest to simple would be to have different STATUS column names, but even this seems extreme.

+3
source

All Articles