简体   繁体   中英

Error Code: 1052. Column 'datetime' in where clause is ambiguous

I have 15 tables of same column names and want to retrieve data on datetime condition but struggling to find out how to do this.

select * FROM AlbertstreetIN1,
AlbertstreetIN2,
AlbertstreetOUT1,
AshtonRPIN1,
AshtonRPIN2
WHERE datetime BETWEEN "2014-08-31 00:00:00" AND "2014-08-31 23:59:59";

data time is ambiguous for all the table names- error.

table format-- id,camera_id,name,plate,datetime, nationlilty,image_name,image.

If a column is ambiguous than it means more than one table in your query has a column with that name.

So you have to tell the DB which column you mean by adding the table name.

WHERE AlbertstreetIN1.datetime BETWEEN '2014-08-31 00:00:00' 
                                   AND '2014-08-31 23:59:59'

Don't know which table you want in your condition. I just chose one.

And you did not specify how to join your tables. They have to be linked somehow and you need to specify that in your query.

Otherwise you need to union them like this

select * FROM AlbertstreetIN1 WHERE datetime BETWEEN '2014-08-31 00:00:00' 
                                   AND '2014-08-31 23:59:59'
UNION ALL
select * FROM AlbertstreetIN2 WHERE datetime BETWEEN '2014-08-31 00:00:00' 
                                   AND '2014-08-31 23:59:59'
UNION ALL
select * FROM AlbertstreetOUT1 WHERE datetime BETWEEN '2014-08-31 00:00:00' 
                                   AND '2014-08-31 23:59:59'
UNION ALL
select * FROM AshtonRPIN1 WHERE datetime BETWEEN '2014-08-31 00:00:00' 
                                   AND '2014-08-31 23:59:59'
UNION ALL
select * FROM AshtonRPIN2WHERE datetime BETWEEN '2014-08-31 00:00:00' 
                                   AND '2014-08-31 23:59:59'

Use table name or alias name in where condition. Since datetime is present in more than one tables you can not use it in WHERE condition without table name or its alias.

like AlbertstreetIN1.datetime .

Can you try like this?

datetime is a one of keyword / predefined in MYSQL

WHERE datetime BETWEEN "2014-08-31 00:00:00" AND "2014-08-31 23:59:59";

to

WHERE `datetime` BETWEEN "2014-08-31 00:00:00" AND "2014-08-31 23:59:59";

or smething like this

WHERE (AlbertstreetIN1.datetime BETWEEN "2014-08-31 00:00:00" AND "2014-08-31 23:59:59") || (AlbertstreetIN2.datetime BETWEEN "2014-08-31 00:00:00" AND "2014-08-31 23:59:59");

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