MySQL - 根据值返回列

时间:2013-11-07 16:14:08

标签: mysql select

我需要从数据库中检索数据,而我无法更改其结构。 地址有3个不同的字段:

  

个人:client_address_1,client_address_2,client_address_3,client_address_4

     

邮政:client_postaladdress_1,client_postaladdress_2,client_postaladdress_3,client_postaladdress_4

     

公司:client_company_address_1,client_company_address_2,client_company_address_3,client_company_address_4

和一个字段(client_prefered_address),其中包含客户希望收到其通信的地址。

从他们那里我需要检索选择的地址,因此,如果客户端被标记为邮政,它应该返回列:client_postaladdress_1, client_postaladdress_2, client_postaladdress_3, client_postaladdress_4但不返回其他列。

有什么办法吗?我一直在谷歌搜索两天。

由于

SQL:

CREATE TABLE IF NOT EXISTS `client` (
  `client_id` int(11) NOT NULL AUTO_INCREMENT,
  `client_address_1` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_address_2` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_address_3` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_address_4` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_postaladdress_1` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_postaladdress_2` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_postaladdress_3` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_postaladdress_4` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_company_address_1` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_company_address_2` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_company_address_3` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_company_address_4` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_prefered_address` char(10) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`client_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4;

--
-- Dumping data for table `client`
--

INSERT INTO `client` (`client_id`, `client_address_1`, `client_address_2`, `client_address_3`, `client_address_4`, `client_postaladdress_1`, `client_postaladdress_2`, `client_postaladdress_3`, `client_postaladdress_4`, `client_company_address_1`, `client_company_address_2`, `client_company_address_3`, `client_company_address_4`, `client_prefered_address`) VALUES
(1, 'Yellow house', 'Yellow street, 25', '09090 Yellow city', 'Yellow Country', 'Blue postbox', 'Blue avenue, 90', '09039 Blue city', 'Blue Country', 'Green house', 'Green street, 100', '02930 Green city', 'Green Country', 'Postal'),
(2, 'Apple house', 'Apple street, 200', 'Apple State 2039', 'Apple Land', 'Melon House', 'Melon Boulevard ', 'Melon State ', 'Melon Land', '', '', '', '', 'Personal'),
(3, '', '', '', '', '', '', '', '', 'Chocolate Factory', 'Charlie street 293', 'Chocolate CH', 'Chocolate Kingdom ', 'Company');

3 个答案:

答案 0 :(得分:1)

我建议使用2个查询来解决此问题。一个用于获取首选地址,另一个用于获取正确的字段。使用您正在使用的任何编程语言将第二个查询与正确的字段一起使用。

如果确实想要使用MySQL,您可以使用CASE语句(就像switch):

SELECT client_id, (
  CASE client_prefered_address
  WHEN 'Postal' THEN client_postaladdress_1
  WHEN 'Personal' THEN client_address_1
  WHEN 'Company' THEN client_company_address_1
END) AS address_1, (
  CASE client_prefered_address
  WHEN 'Postal' THEN client_postaladdress_2
  WHEN 'Personal' THEN client_address_2
  WHEN 'Company' THEN client_company_address_2
END) AS address_2, (
  CASE client_prefered_address
  WHEN 'Postal' THEN client_postaladdress_3
  WHEN 'Personal' THEN client_address_3
  WHEN 'Company' THEN client_company_address_3
END) AS address_3, (
  CASE client_prefered_address
  WHEN 'Postal' THEN client_postaladdress_4
  WHEN 'Personal' THEN client_address_4
  WHEN 'Company' THEN client_company_address_4
END) AS address_4

FROM client

DEMO:http://sqlfiddle.com/#!2/eab6b/3

答案 1 :(得分:1)

使用CASE

尝试此操作
select 

(CASE WHEN client_prefered_address ='Postal' 
THEN client_postaladdress_1 
WHEN client_prefered_address ='Personal' THEN client_address_1
WHEN client_prefered_address ='Company' THEN client_company_address_1
ELSE NULL END) `addressone`
,
(CASE WHEN client_prefered_address ='Postal' 
THEN client_postaladdress_2 
WHEN client_prefered_address ='Personal' THEN client_address_2
WHEN client_prefered_address ='Company' THEN client_company_address_2
ELSE NULL END) `addresstwo`,

(CASE WHEN client_prefered_address ='Postal' 
THEN client_postaladdress_3 
WHEN client_prefered_address ='Personal' THEN client_address_3
WHEN client_prefered_address ='Company' THEN client_company_address_3
ELSE NULL END) `addressthree`,

(CASE WHEN client_prefered_address ='Postal' 
THEN client_postaladdress_4 
WHEN client_prefered_address ='Personal' THEN client_address_4
WHEN client_prefered_address ='Company' THEN client_company_address_4
ELSE NULL END) `addressfour`

FROM `client`

FIDDLE

答案 2 :(得分:0)

尝试这样的事情:

SELECT `client_id`,
    IF(`client_prefered_address` = 'personal', `client_address_1`, IF(`client_prefered_address` = 'postal', `client_postaladdress_1`, `client_company_address_1`) AS `address_1`,
    IF(`client_prefered_address` = 'personal', `client_address_2`, IF(`client_prefered_address` = 'postal', `client_postaladdress_2`, `client_company_address_2`) AS `address_2`,
    IF(`client_prefered_address` = 'personal', `client_address_3`, IF(`client_prefered_address` = 'postal', `client_postaladdress_3`, `client_company_address_3`) AS `address_3`,
    IF(`client_prefered_address` = 'personal', `client_address_4`, IF(`client_prefered_address` = 'postal', `client_postaladdress_4`, `client_company_address_4`) AS `address_4`
FROM `client`
WHERE blahblahblah