来自两个表的MySQL数据:将列标题添加到输出

时间:2018-11-09 10:32:18

标签: mysql inner-join

我使用以下方法从两个表中获取数据:

SELECT p.id, p.title, p.event_date, a.name, p.location_id 
FROM ixrsk_eb_events p 
INNER JOIN ixrsk_eb_locations a on p.location_id = a.id

这很好。

此外,我现在想要列标题。 在结果之上:“ ID”(col p.id),“ Title”(col p.title),“ Date”(col p.event_date)和“ Location”(col {{1 }})。

a.name根本不应该显示(无标题,无数据)。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用别名来更改显示的列的名称:

SELECT 
  p.id AS ID, 
  p.title AS Title, 
  p.event_date AS Date, 
  a.name AS Location
FROM ixrsk_eb_events p 
INNER JOIN ixrsk_eb_locations a on p.location_id = a.id

,只删除不想显示的列。

答案 1 :(得分:0)

您正在寻找别名的列/表达式。请参阅本教程以获取更多说明:http://www.mysqltutorial.org/mysql-alias/

SELECT p.id AS ID, 
       p.title AS Title, 
       p.event_date AS Date, 
       a.name AS Location
       -- removed p.location_id 
FROM ixrsk_eb_events p 
INNER JOIN ixrsk_eb_locations a on p.location_id = a.id

要跳过特定列,只需要将其从SELECT子句中删除。