1052: Column 'Id' in Field List Is Ambiguous

1052: Column 'id' in field list is ambiguous

SQL supports qualifying a column by prefixing the reference with either the full table name:

SELECT tbl_names.id, tbl_section.id, name, section
FROM tbl_names
JOIN tbl_section ON tbl_section.id = tbl_names.id

...or a table alias:

SELECT n.id, s.id, n.name, s.section
FROM tbl_names n
JOIN tbl_section s ON s.id = n.id

The table alias is the recommended approach -- why type more than you have to?

Why Do These Queries Look Different?

Secondly, my answers use ANSI-92 JOIN syntax (yours is ANSI-89). While they perform the same, ANSI-89 syntax does not support OUTER joins (RIGHT, LEFT, FULL). ANSI-89 syntax should be considered deprecated, there are many on SO who will not vote for ANSI-89 syntax to reinforce that. For more information, see this question.

Error Code: 1052. Column 'TradeDate' in field list is ambiguous

your last query should use the explicit join syntax and should always have the table.field notation to avoid the error you are referring

SELECT pricetemp.TradeDate, Price, Temp, Consumption
FROM pricetemp JOIN consumption ON
pricetemp.TradeDate = consumption.TradeDate

or

SELECT consumption.TradeDate, Price, Temp, Consumption
FROM pricetemp JOIN consumption ON
pricetemp.TradeDate = consumption.TradeDate

To join another table just:

SELECT consumption.TradeDate, Price, Temp, Consumption, anotherTable.production
FROM pricetemp
JOIN consumption ON pricetemp.TradeDate = consumption.TradeDate
JOIN anotherTable ON pricetemp.TradeDate = anotherTable.TradeDate

ERROR 1052 (23000) at line 1: Column 'id' in field list is ambiguous

Most likely both the blog and site tables have an id column. You should explicitly list out all columns you want to select:

SELECT
b.id AS b_id,
s.id AS s_id,
b.siteId,
s.expiryDate
-- plus anything else you want to select
FROM blog b
INNER JOIN site s
ON s.id = b.siteId
WHERE
s.expiryDate < '2021-03-01' AND
b.plagiarismStatus IN (1, 9) AND
b.isDeleted = 0 AND
b.isClosed = 1;

When you used SELECT * you were selecting an id column from each of the two tables. MySQL is rolling over and telling you that it doesn't know how to make sense of this.

Mysqli - #1052 - Column 'id' in field list is ambiguous

Some simple rules when writing SQL:

  • Never use commas in the FROM clause.
  • Always use explicit, proper JOIN syntax.
  • Always alias your tables with abbreviations.
  • Always qualify your column references.

Your resulting queries are more likely to work the first time:

SELECT c.id, c.firstName, c.lastName, SUM(o.qty * m.price)
FROM orders o JOIN
menu m
ON m.id = o.id_menu JOIN
customers c
ON c.id = o.id_customer
GROUP BY c.id, c.firstName, c.lastName
ORDER BY SUM(o.qty * m.price)
LIMIT 1;

Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous

That's because your KhatmaRead and Khatma models has update_at column and here:

->select('reader_name','message','updated_at')

You don't specify from what table you will take that column and it's ambiguous. You should do something like this:

->select('reader_name','message','khatma_reads.updated_at')


Related Topics



Leave a reply



Submit