我有两个表 base_list 和 entries_table
base_list中包含所有菜肴的名称,ID和名称-这是主列表。
entries_table是一个包含10列的表。这些列对应于人们投票时所进行的排名。例如,John Doe将选择10个菜肴,然后将这些菜肴的ID保存在entry_table中。只要有人投票给10道菜,它就会保存在这张桌子上。
我想创建一个查询,在该查询中将以entrys_table结尾,但以ID代替,而以菜名结尾。
到目前为止,我已经尝试了内部联接,但尚未成功。我不太确定该怎么做。
答案 0 :(得分:1)
您的桌子设计使事情变得复杂。您的entry_table应该看起来像这样:
user_ID, dish_ID, ranking, date, selection_ID
(如果某人每个日期进行多个选择,则需要selection_ID之类的东西
在当前设计中,您可以使用包含所有菜肴的if子句。但是,根据菜肴的数量,可能需要太多工作。
但这应该对您有用:
Select b.dish_name as dish_1,
c.dish_name as dish_2,
d.dish_name as dish_3,
e.dish_name as dish_4,
f.dish_name as dish_5,
g.dish_name as dish_6,
h.dish_name as dish_7,
i.dish_name as dish_8,
j.dish_name as dish_9,
k.dish_name as dish_10,
from entries_table a
left join base_list as b on(a.dish_id_1 = b.dish_id)
left join base_list as c on(a.dish_id_2 = c.dish_id)
left join base_list as d on(a.dish_id_3 = d.dish_id)
left join base_list as e on(a.dish_id_4 = e.dish_id)
left join base_list as f on(a.dish_id_5 = f.dish_id)
left join base_list as g on(a.dish_id_6 = g.dish_id)
left join base_list as h on(a.dish_id_7 = h.dish_id)
left join base_list as i on(a.dish_id_8 = i.dish_id)
left join base_list as j on(a.dish_id_9 = j.dish_id)
left join base_list as k on(a.dish_id_10 = k.dish_id)
我不知道您的确切列名。只需将我的列名替换为您的列名,然后在需要时添加更多列即可,它应该会为您提供所需的结果。