[英]left join to get min date
I want to get the first date of each booking in my database. 我想在数据库中获取每次预订的第一个日期。
Table BOOKINGS is like: 表预订类似于:
id | id_client_booking | the_date
Table CLIENT_BOOKINGS is 表CLIENT_BOOKINGS是
id | etc | etc
on BOOKINGS
each id_client_booking
correponds to one id
of CLIENT_BOOKINGS
, and can have many dates. 在
BOOKINGS
每个id_client_booking
correponds一个id
的CLIENT_BOOKINGS
,并且可以有很多的日期。
I would like to get the first date of each client booking. 我想得到每个客户预订的第一个日期。 This select gives me just the first date of all client bookings.
此选择仅给我所有客户预订的第一个日期。 What am I missing?
我想念什么?
SELECT
min(the_date) AS start_date, id_client_booking
FROM `day_bookings`AS db
LEFT JOIN `client_bookings`AS cb ON db.id_client_booking = cb.id
Tried also: 也尝试过:
SELECT
min(the_date) AS db.start_date, db.id_client_booking
^^ ^^
FROM `day_bookings`AS db
LEFT JOIN `client_bookings`AS cb ON db.id_client_booking = cb.id
but that gave error: You have an error in your SQL syntax;
但这带来了错误:
You have an error in your SQL syntax;
You are missing a GROUP BY
, try this: 您缺少
GROUP BY
,请尝试以下操作:
SELECT MIN(the_date) AS start_date, id_client_booking
FROM `day_bookings`AS db
LEFT JOIN `client_bookings`AS cb
ON db.id_client_booking = cb.id
GROUP BY id_client_booking
You can only use AS
key word to define names for expressions or aliases. 您只能使用
AS
关键字来定义表达式或别名的名称。
You can't use it to point to a column directly. 您不能使用它直接指向列。 And hence is the syntax error.
因此是语法错误。
SELECT
min(the_date) AS db.start_date, db.id_client_booking
^^ ^^
FROM `day_bookings`AS db
LEFT JOIN `client_bookings`AS cb ON db.id_client_booking = cb.id
It should be: 它应该是:
SELECT
min(the_date) AS 'start_date', db.id_client_booking
FROM `day_bookings`AS db
LEFT JOIN `client_bookings`AS cb ON db.id_client_booking = cb.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.