[英]1052: Column 'id' in field list is ambiguous
I have 2 tables.我有 2 张桌子。
tbl_names
and tbl_section
which has both the id
field in them. tbl_names
和tbl_section
都包含id
字段。 How do I go about selecting the id
field, because I always get this error:我如何 go 关于选择
id
字段,因为我总是得到这个错误:
1052: Column 'id' in field list is ambiguous
Here's my query:这是我的查询:
SELECT id, name, section
FROM tbl_names, tbl_section
WHERE tbl_names.id = tbl_section.id
I could just select all the fields and avoid the error.我可以只 select 所有字段并避免错误。 But that would be a waste in performance.
但这会浪费性能。 What should I do?
我应该怎么办?
SQL supports qualifying a column by prefixing the reference with either the full table name: SQL 支持通过在引用前面加上完整的表名来限定列:
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?表别名是推荐的方法——为什么输入比你必须输入的更多?
Secondly, my answers use ANSI-92 JOIN syntax (yours is ANSI-89).其次,我的答案使用 ANSI-92 JOIN 语法(你的是 ANSI-89)。 While they perform the same, ANSI-89 syntax does not support OUTER joins (RIGHT, LEFT, FULL).
虽然它们执行相同,但 ANSI-89 语法不支持 OUTER 连接(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.
ANSI-89 语法应被视为已弃用,SO 上有很多人不会投票支持 ANSI-89 语法来加强这一点。 For more information, see this question .
有关详细信息,请参阅此问题。
In your SELECT
statement you need to preface your id with the table you want to choose it from.在您的
SELECT
语句中,您需要在您的 id 前面加上您要从中选择的表。
SELECT tbl_names.id, name, section
FROM tbl_names
INNER JOIN tbl_section
ON tbl_names.id = tbl_section.id
OR或者
SELECT tbl_section.id, name, section
FROM tbl_names
INNER JOIN tbl_section
ON tbl_names.id = tbl_section.id
You would do that by providing a fully qualified name, eg:您可以通过提供完全限定的名称来做到这一点,例如:
SELECT tbl_names.id as id, name, section FROM tbl_names, tbl_section WHERE tbl_names.id = tbl_section.id
Which would give you the id of tbl_names这会给你 tbl_names 的 id
The simplest solution is a join with USING
instead of ON
.最简单的解决方案是
USING
而不是ON
加入。 That way, the database "knows" that both id
columns are actually the same, and won't nitpick on that:这样,数据库“知道”两个
id
列实际上是相同的,并且不会对此进行挑剔:
SELECT id, name, section
FROM tbl_names
JOIN tbl_section USING (id)
If id
is the only common column name in tbl_names
and tbl_section
, you can even use a NATURAL JOIN
:如果
id
是tbl_names
和tbl_section
中唯一常见的列名,您甚至可以使用NATURAL JOIN
:
SELECT id, name, section
FROM tbl_names
NATURAL JOIN tbl_section
See also: https://dev.mysql.com/doc/refman/5.7/en/join.html另请参阅: https://dev.mysql.com/doc/refman/5.7/en/join.html
What you are probably really wanting to do here is use the union operator like this:您可能真正想要在这里做的是像这样使用联合运算符:
(select ID from Logo where AccountID = 1 and Rendered = 'True')
union
(select ID from Design where AccountID = 1 and Rendered = 'True')
order by ID limit 0, 51
Here's the docs for it https://dev.mysql.com/doc/refman/5.0/en/union.html这是它的文档https://dev.mysql.com/doc/refman/5.0/en/union.html
Already there are lots of answers to your question, You can do it like this also.您的问题已经有很多答案,您也可以这样做。 You can give your table an alias name and use that in the select query like this:
您可以给您的表一个别名,并在 select 查询中使用它,如下所示:
SELECT a.id, b.id, name, section
FROM tbl_names as a
LEFT JOIN tbl_section as b ON a.id = b.id;
If the format of the id's in the two table varies then you want to join them, as such you can select to use an id from one-main table, say if you have table_customes
and table_orders
, and tha id for orders is like " 101 "," 102 "..." 110 ", just use one for customers如果两个表中 id 的格式不同,那么你想加入它们,因此你可以 select 使用一个主表中的 id,比如你有
table_customes
和table_orders
,订单的 id 就像“ 101 "," 102 "..." 110 ", 只给客户一个
select customers.id, name, amount, date from customers.orders;
SELECT tbl_names.id, tbl_names.name, tbl_names.section
FROM tbl_names, tbl_section
WHERE tbl_names.id = tbl_section.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.