Running MySQL query for equality between ID, in a table that has multiple IDs

I have two MySQL tables

One I need all the columns, let's call it totalorder, the other one I just need one column, which is a date, let's call it orderproduct. I can match them by a column called order_id. The thing is, table1 has multiple entries for order_id, whilst table2 has just one entry. I need the date from totalorder in multiple rows in orderproduct.

Here is the MySQL query I've written so far:

SELECT
    op.*, op.`date_created`
FROM
    orderproduct op, totalorder to
WHERE
    op.order_id = to.order_id 

When I ran this query, I get error #1064.

What am I doing wrong?

Upvotes: 0

Views: 19

Answers (1)

Jens
Jens

Reputation: 69450

to is a keyword in mysql. so you can not use it as an alias, or you have to escape it using backticks.

SELECT
    op.*, op.`date_created`
FROM
    orderproduct op, totalorder tot
WHERE
    op.order_id = tot.order_id 

or

SELECT
    op.*, op.`date_created`
FROM
    orderproduct op, totalorder `to`
WHERE
    op.order_id = `to`.order_id 

Upvotes: 2

Related Questions