未使用SELECT查询显示所有选定的字段 - MySQL

时间:2017-11-03 15:15:15

标签: mysql

我正在尝试使用自定义值从表中选择一些字段,当我运行select查询时,它会显示前两个选定的列,而不管选择查询中的顺序(如country,name,slug,area_slug或country,slug, area_slug,名称)。

请找到表格架构和下面的查询。

TABLE:
    CREATE TABLE `destination` (
      `code` varchar(5) NOT NULL,
      `country` varchar(2) NOT NULL,
      `region` varchar(50) NOT NULL,
      `name` varchar(50) NOT NULL,
      `parent` varchar(5) DEFAULT NULL,
      `latitude` double(30,15) NOT NULL,
      `longitude` double(30,15) NOT NULL,
      `updated` varchar(30) NOT NULL DEFAULT '0000-00-00T00:00:00.000000Z'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

QUERY:

SELECT 'ae' as country,
       name,
       replace(lcase(name), ' ', '-') as slug,
       CONCAT(replace(lcase(name), ' ', '-'),'-united-arab-emirates') as area_slug  
FROM `destination` 
WHERE code IN ("18ed5","18ed6","18ed7","18ed8","18ed9","18eda","18edb")

1 个答案:

答案 0 :(得分:1)

enter image description here

根据提供的屏幕截图中的错误消息,您在查询中没有唯一列,这是真的。表中似乎没有一个。我建议您在表中添加一个自动递增ID,然后在查询中包含该列。

例如:

DROP TABLE IF EXISTS `destination`;

CREATE TABLE `destination` (

  `id` mediumint(8) unsigned NOT NULL auto_increment primary key,

      `code` varchar(5) NOT NULL,
      `country` varchar(2) NOT NULL,
      `region` varchar(50) NOT NULL,
      `name` varchar(50) NOT NULL,
      `parent` varchar(5) DEFAULT NULL,
      `latitude` double(30,15) NOT NULL,
      `longitude` double(30,15) NOT NULL,
      `updated` varchar(30) NOT NULL DEFAULT '0000-00-00T00:00:00.000000Z'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

INSERT INTO     `destination` (code,country,region,name,parent,latitude,longitude,updated)
VALUES ('18ed5','1','1','1','1','1',1,2);

SELECT   ID
       , 'ae'                                                            AS country 
       , name 
       , REPLACE(Lcase(name), ' ', '-')                                  AS slug 
       , Concat(REPLACE(Lcase(name), ' ', '-'), '-united-arab-emirates') AS area_slug 
FROM   `destination` 
WHERE  code IN ( '18ed5', '18ed6', '18ed7', '18ed8','18ed9', '18eda', '18edb' ) 

<强>结果

| ID | country | name | slug |       area_slug        |
|----|---------|------|------|------------------------|
|  1 | ae      |    1 |    1 | 1-united-arab-emirates |

请参阅:http://rextester.com/LER16583