2

I want to use inner join and order in my query in php page.
My query :

select 
    * 
from 
    table1 
inner join 
    table1category 
    on table1.table1category_id = table1category.id
order by updateDate desc;

'updateDate' is for table1 => error : Column 'updateDate' in order clause is ambiguous

3
  • 4
    The error message says it all. You need to specify which updateDate to order by. E.g. try order by table1.updateDate desc.
    – jarlh
    Commented Aug 8, 2018 at 9:32
  • 3
    you need to add table_name, like order by table1.updateDate Commented Aug 8, 2018 at 9:32
  • 2
    Possible duplicate of Ambigous column name error
    – Tobias F.
    Commented Aug 8, 2018 at 9:34

9 Answers 9

7

Column 'updateDate' in order clause is ambiguous

Means that updateDate exists in both tables you are trying to use. If you want to order using this field, you have to specify from which table you want it to be ordered by.

For example:

SELECT
  *
FROM table1
INNER JOIN table1category
  ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;

Also, consider that using * on a SELECT with JOINS will get all the columns from all the included tables.

3
  • 3
    It's weird to see that every one else is duplicating the same after you answered.
    – nice_dev
    Commented Aug 8, 2018 at 9:37
  • 2
    well... ¯_(ツ)_/¯
    – Rafael
    Commented Aug 8, 2018 at 9:38
  • 1
    you have to specify every field you want to the SELECT instead of *, this also improve the performance of the query.
    – Rafael
    Commented Aug 8, 2018 at 9:56
5

when there is same column in two table then you've to specify the table name with that columns

SELECT
  *
FROM table1
INNER JOIN table1category
  ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;
1
  • select table1.rowid
    – Fahmi
    Commented Aug 8, 2018 at 9:56
4

Its because updateDate exists in both tables- table1 and table1category

SELECT
  *
FROM table1 t
INNER JOIN table1category tc
  ON t.table1category_id = tc.id
ORDER BY t.updateDate DESC;
1
  • By specifically naming the columns that you need to use in your select or simply use alias naming to differentiate between the two Commented Oct 8, 2018 at 9:54
2

You have to say which table to use for this field (presents in both tables):

select *
from table1
inner join table1category on table1.table1category_id = table1category.id
order by XXX.updateDate desc;

Replace XXX by table1 or table1category.

You can set aliases on tables too, like:

select *
from table1 as myalias1
inner join table1category as myalias2 on table1.table1category_id = table1category.id
order by XXX.updateDate desc;

Then replace XXX by myalias1 or 2.

You can do the same for selecting datas (for example):

SELECT table1.id AS id, table2.id AS categoryId
0
2

Column updateDate in order clause is ambiguous

Try This

SELECT
  *
FROM table1 t1
INNER JOIN table1category t2
  ON t1.table1category_id = t2.id
ORDER BY t1.updateDate DESC;
0
2

I find that using aliases often makes joins easier

SELECT
  *
FROM table1 AS alias1
INNER JOIN table1category AS alias2
  ON alias1.table1category_id = alias2.id
ORDER BY alias1.updateDate DESC;
0
1

You are missing table name

SELECT
  *
FROM table1
INNER JOIN table1category
  ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;
0
0
SELECT *, table1.updateDate as "Table1 Update Date"
FROM table1 
INNER JOIN table1category ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;

or you can keep alias in ORDER BY clause

SELECT *, table1.updateDate as "Table1 Update Date"
FROM table1 
INNER JOIN table1category ON table1.table1category_id = table1category.id
ORDER BY "Table1 Update Date" DESC;

Note Use keywords always in UPPER case Better to use alias in SELECT CLAUSE for better understanding, otherwise it will add 1 to updateDate i.e. updateDate1. Always use alias for same column names Don't use all columns i.e. * , fetch only required columns

0

select * from table1 T inner join table1category C on T.table1category_id = C.id order by T.updateDate desc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.