查询将列添加到单行中

时间:2012-11-12 05:13:25

标签: mysql sql

我有一张如下表格


    CREATE TABLE `fruits` (
      `fruit_id` int(11) default NULL,
      `fruit_name` varchar(255) collate latin1_general_ci default NULL
    );

该表包含以下字段

    INSERT INTO fruits(fruit_id, fruit_name) 
               VALUES (101, 'Mango'),
                      (102, 'Apple'),
                      (103, 'Orange'),
                      (104, 'Pineapple'),
                      (105, 'Lemon'),
                      (106, 'Custard');

我想要一个select查询,它将列引入单行,如下所示 喜欢

    SELECT fruit_id, fruit_name
       FROM fruits
      WHERE fruit_id = 101

以上查询将输出带入两列

    101    Mango

我希望单行中的列如下


    101
    Mango

1 个答案:

答案 0 :(得分:1)

SELECT row
FROM
(
  SELECT fruit_id, fruit_id row FROM fruits
  UNION
  SELECT fruit_id, fruit_name row FROM fruits
) s
WHERE fruit_id = 101;