简体   繁体   English

where子句中的列'id_f'是不明确的

[英]Column 'id_f' in where clause is ambiguous

i have a join query using 3 table but i get this problem Column 'id_f' in where clause is ambiguous 我有一个使用3表的连接查询,但我得到这个问题列'id_f'在where子句是不明确的

$id_f=$_GET['id_f'];

$query="SELECT *, s.name AS van, st.name AS naar, p.titl, p.vname
FROM p1_users, f_confirm AS v 
INNER JOIN s_steden AS s ON v.van = s.id
INNER JOIN s_steden AS st ON v.naar = st.id
INNER JOIN p1_users AS p ON v.id_f = p.id_f
AND DATE_FORMAT(date,'%Y-%c-%d')WHERE id_f='$id_f'";
$result=mysql_query($query)or die('Wrong query : ' . mysql_error());
$row=mysql_fetch_assoc($result);

can anyone help? 有人可以帮忙吗?

It means, that two or more tables contain column with name "id_f"(in your case that are p1_users and f_confirm ). 这意味着,两个或多个表包含名为“id_f”的列(在您的情况下为p1_users和f_confirm)。 You need to specify for which table it related, something like this: 您需要指定它与哪个表相关,如下所示:

AND DATE_FORMAT(date,'%Y-%c-%d')WHERE p.id_f='$id_f'";

You need to use v.id_f or p.id_f in your where clause as two tables have a column of that name so you need to disambiguate. 您需要在where子句中使用v.id_fp.id_f ,因为两个表具有该名称的列,因此您需要消除歧义。

It actually doesn't matter which one you use in this case as you are doing an inner join . 在这种情况下,当你正在进行inner join使用哪一个并不重要。

This might not be a requirement if you use a natural join but I don't suggest using these. 如果您使用natural join这可能不是natural join但我不建议使用这些。

An ambiguous field is a field in a query that is in two or more tables (or subqueries). 不明确的字段是查询中的字段,该字段位于两个或多个表(或子查询)中。

you have two options. 你有两个选择。

  1. Use table_name prefix on each ambiguous field (table1.field_x, table2.field_x) 在每个不明确的字段上使用table_name前缀(table1.field_x,table2.field_x)
  2. Use table alias, and use the alias as prefix on each ambiguous field (alias1.x, alias2.x). 使用表别名,并在每个不明确字段(alias1.x,alias2.x)上使用别名作为前缀。

In your example, in the INNER JOIN statement you use the alias for "v.id_f = p.id_f", but then, in the WHERE clause you forget the alias. 在您的示例中,在INNER JOIN语句中使用“v.id_f = p.id_f”的别名,但随后在WHERE子句中忘记了别名。

The problem is in WHERE clause, after DATE_FORMAT you wrote id_f='$id_f', but you have this column (id_f) in other tables in this query. 问题出在WHERE子句中,在DATE_FORMAT之后编写了id_f ='$ id_f',但是在此查询的其他表中有此列(id_f)。 So the database doesn't know wchich table you really want to use. 所以数据库不知道你真正想要使用的wchich表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM