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