MySQL 'tahun_pemilihan' in where clause is ambigous

374 Views Asked by At

How can I correct the problem I keep getting from the code below which states 'tahun_pemilihan' in where the clause is ambiguous. Thanks for the help in advance.

here is the MySQL table

 $sql = "SELECT 
              a.*, b.vektor_v FROM data_alternatif a, hasil b 
    WHERE a.id_alternatif=b.id_alternatif AND tahun_pemilihan='$thn'";
2

There are 2 best solutions below

0
mhawke On BEST ANSWER

Both tables contain a column named tahun_pemilihan so in the WHERE clause condition it is not clear which table's column to use; should it use the one from data_alternatif or from the results table hasil?

Qualify the table in the WHERE clause with either a.tahun_pemilihan='$thn' if it should use column data_alternatif.tahun_pemilihan or b.tahun_pemilihan='$thn' if hasil.tahun_pemilihan is the one to use.

0
Onitech On

You can encounter ambiguous column name error if the column name exist in both tables you have joined.

In your case, the column name tahun_pemilihan exist in both of your table.

Solution:

Pick which one of the two table you will use. a.tahun_pemilihan='$thn' or b.tahun_pemilihan='$thn'

https://www.quora.com/What-does-the-SQL-ambiguous-column-name-error-mean