我正在尝试从数据库中检索数据,我加入了另一个表,该表需要从该列中获取数据,但又分为多个列。
到目前为止,我有此代码:
SELECT
restaurants.name AS 'Name',
extras_time_slots.restaurant_id AS 'DC ID',
SUM(capacity) AS 'Seats opened',
meal_group_name AS 'Category',
CASE
WHEN extras_events.status = 0 THEN 'Offline'
WHEN extras_events.status = 1 THEN 'Submitted'
WHEN extras_events.status = 2 THEN 'Online'
ELSE 'Unknown'
END AS 'Status',
CASE
WHEN restaurants.region_id = 29 THEN 'Beijing'
WHEN restaurants.region_id = 31 THEN 'Shanghai'
WHEN restaurants.region_id = 63 THEN 'Guangzhou'
WHEN restaurants.region_id = 83 THEN 'Shenzhen'
WHEN restaurants.region_id = 86 THEN 'Hangzhou'
WHEN restaurants.region_id = 88 THEN 'Suzhou'
WHEN restaurants.region_id = 91 THEN 'Chengdu'
ELSE 'Unknown'
END AS 'City',
(
SELECT extras_event_meals.price
FROM extras_event_meals
WHERE meal_type LIKE 'lunch'
AND extras_events_id
) AS 'Lunch price',
(
SELECT extras_event_meals.price
FROM extras_event_meals
WHERE meal_type LIKE 'dinner'
AND extras_events_id
) AS 'Dinner price',
(
SELECT extras_event_meals.price
FROM extras_event_meals
WHERE meal_type LIKE 'brunch'
AND extras_events_id
) AS 'Brunch price'
FROM extras_time_slots
JOIN restaurants
ON extras_time_slots.restaurant_id = restaurants.id
JOIN extras_events
ON extras_events.restaurant_id = restaurants.id AND extras_events.project = extras_time_slots.project
JOIN extras_event_meals
ON extras_event_meals.extras_event_id = extras_events.id
WHERE extras_time_slots.project = 'rwcn_autumn_2018'
GROUP BY restaurants.id;
因此,对于一个餐厅,表extras_events_meal_price具有多个具有类型和价格的“餐”。
以下是各列:
id
extras_events_id
meal_type
price
created_at
updated_at
一个餐厅可以有多个进餐类型和价格,但是它们在同一列中,因此,在我的上一个SELECT子查询中,我试图将它们分为三列:早午餐价格,午餐价格,晚餐价格。
我一直得到的错误是:
子查询返回多于1行。
答案 0 :(得分:3)
您从未在子选择语句中完成查询。 extras_events_id
应该等于extras_events.id
。通过不包括它,您只是在检查它是否为true
,它将返回表中每个meal_type
的“午餐”:
SELECT extras_event_meals.price
FROM extras_event_meals
WHERE meal_type LIKE 'lunch'
AND extras_event_id = extras_events.id