简体   繁体   中英

column name is ambiguous mysql

I have spent hours trying to select all rows from different tables and still no luck. I am getting:

column name firstname is ambiguous 

The tables don't have any relationships, I just want to select all rows as I need the query for searching all tables for matching string. I have search bar.

Is there any nicer way to select all rows from multiple tables where the column numbers doesn't match. I have tried UNION ALL but it gives me an error. Please help

 $mydb = new mysqli('localhost', 'root', '', 'db');
$stmt = $mydb->prepare("SELECT * FROM `table1` t1 JOIN `table2` t2 ON joinitem.t1 = joinitem.t2 where firstname = ? ");
stmt->bind_param('s', $firstname);
echo $mydb->error;
$stmt->execute();

try being specific in WHERE clause, like

SELECT * FROM `table1` t1 
   JOIN `table2` t2 ON t1.joinitem = t2.joinitem
   WHERE t1.firstname = ? 

That error came up because you have the column firstname in both your tables. Without specifying which firstname you mean, SQL cannot tell whether you want the query to be based on table1's firstname or table2's firstname.

To make sure SQL knows which one you mean, prefix the column name with the table name.

Also, I believe the JOIN condition should be tablename.joinitem instead of joinitem.tablename .

Here is an example assuming the firstname you want is from table2 :

SELECT *
FROM `table1` t1
JOIN `table2` t2 ON t1.joinitem = t2.joinitem
WHERE t2.firstname = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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