我有以下的數據庫結構:如何從三個表在一個查詢中選擇
db_config_list id
| id_config
| type
DB_CONFIG id
| id_list
| name
| value
db_config_lang id
| id_list
| id_lang
| name
| value
在一個MySQL的查詢我想從db_config
和db_config_lang
其中db_config.id_list = db_config_list.id
和db_config_lang.id_list = db_config_list.id
我試過選擇的東西:
$sql = 'SELECT * FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
ON (cl.id = c.id_list)
LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
ON (cl.id = cll.id_list)
WHERE cl.id_config = '.$this->c_id.'
AND cl.type = "'.$this->c_type.'"
AND cll.id_lang = "'.$this->default_lang.'"';
,但它不工作正確的。這是實際工作我做的時候這兩個不同的查詢:
$sql = 'SELECT * FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
ON (cl.id = c.id_list)
WHERE cl.id_config = '.$this->c_id.'
AND cl.type = "'.$this->c_type.'"';
$sql = 'SELECT * FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
ON (cl.id = cll.id_list)
WHERE cll.id_lang = "'.$this->default_lang.'"';
但我想這樣做在一個查詢。這可能嗎 ?
//編輯
我想我是想實現的東西是不可能的。所以我選擇了不同的解決方案
$sql = 'SELECT name,value FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
LEFT JOIN `'._DB_PREFIX_.$this->db_config.'` c
ON (cl.id = c.id_list)
WHERE cl.id_config = '.$this->c_id.'
AND cl.type = "'.$this->c_type.'"';
$sql2 = 'SELECT name,value FROM `'._DB_PREFIX_.$this->db_config_list.'` cl
LEFT JOIN `'._DB_PREFIX_.$this->db_config_lang.'` cll
ON (cl.id = cll.id_list)
WHERE cll.id_lang = "'.$this->default_lang.'"';
$query1 = Db::getInstance()->executeS($sql);
$query2 = Db::getInstance()->executeS($sql2);
$query = array_merge($query1, $query2);
// EDIT 2
這裏是sqlfiddle.com
創建代碼代碼:
CREATE TABLE IF NOT EXISTS `db_config_list` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_config` int(10) unsigned NOT NULL,
`type` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS `db_config` (
`id_list` int(10) unsigned NOT NULL,
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`value` text NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS `db_config_lang` (
`id_list` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL,
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`value` text NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;
ALTER TABLE `db_config` ADD INDEX(`id_list`);
ALTER TABLE `db_config_lang` ADD INDEX(`id_list`);
ALTER TABLE `db_config` ADD FOREIGN KEY (`id_list`) REFERENCES `db_config_list`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE `db_config_lang` ADD FOREIGN KEY (`id_list`) REFERENCES `db_config_list`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
INSERT INTO `db_config_list` (`id`, `id_config`, `type`) VALUES
(1, 1, 'global');
INSERT INTO `db_config` (`id_list`, `id`, `name`, `value`) VALUES
(1, 1, 'font_family', ''),
(1, 2, 'first_main_color', '#19BCE7'),
(1, 3, 'use_background_image', '1'),
(1, 4, 'background_color', '#F60'),
(1, 5, 'animated_tabs_carousel', '1'),
(1, 6, 'boxed_layout', '1'),
(1, 7, 'loading_animation', '1'),
(1, 8, 'smooth_scroll', '1'),
(1, 9, 'responsiveness', '1'),
(1, 10, 'sticky_header', '1');
INSERT INTO `db_config_lang` (`id_list`, `id_lang`, `id`, `name`, `value`) VALUES
(1, 1, 1, 'center_column_content', 'boo foo'),
(1, 1, 2, 'bottom_column_content', 'boo foo'),
(1, 2, 3, 'center_column_content', 'boo foo'),
(1, 2, 4, 'bottom_column_content', 'boo foo'),
(1, 3, 5, 'center_column_content', 'boo foo'),
(1, 3, 6, 'bottom_column_content', 'boo foo'),
(1, 4, 7, 'center_column_content', 'boo foo'),
(1, 4, 8, 'bottom_column_content', 'boo foo');
選擇代碼:
SELECT name,value FROM `db_config_list` cl
LEFT JOIN `db_config` c
ON (cl.id = c.id_list)
WHERE cl.id_config = 1
AND cl.type = "global";
SELECT name,value FROM `db_config_list` cl
LEFT JOIN `db_config_lang` cll
ON (cl.id = cll.id_list)
WHERE cll.id_lang = 1
AND cl.id_config = 1
AND cl.type = "global";
這可能是可能的。我會檢查你的想法。 Thx回覆, –
@WalterWhite,如果它解決了您的問題,請記住接受Mikel的回答。 – SpencerD
@Mikel Bitson你睜開了眼睛。問題解決了,我更新了這篇文章。謝謝。 –