0

我需要从数据库中检索数据,但我无法更改其结构。地址有 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');
4

3 回答 3

1

我建议使用 2 个查询来解决这个问题。一个获取首选地址,另一个获取正确的字段。使用您使用的任何编程语言来生成具有正确字段的第二个查询。

如果你真的想使用 MySQL,你可以使用CASE语句(就像 a 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

演示:http ://sqlfiddle.com/#!2/eab6b/3

于 2013-11-07T16:25:42.660 回答
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`

小提琴

于 2013-11-07T16:25:55.320 回答
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
于 2013-11-07T16:20:10.690 回答