2015-05-27 54 views
2

我有以下的數據庫結構:如何從三個表在一個查詢中選擇

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_configdb_config_lang其中db_config.id_list = db_config_list.iddb_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"; 

回答

3

我在想,發生的事情更可能只是select是*屬性 - 它將只爲每個列名選擇一個值,而不管有多少表連接。如果您可以發佈此設置的SQL Fiddle,我可以幫助您修改查詢。從本質上講,我相信你正在尋找這樣做:

$sql = 'SELECT *, c.name AS config_name, cll.name as config_lang_name 
     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 cl.name AS config_list_name, cl.value AS config_list_value, 
     cll.name AS config_lang_name, cll.value AS config_lang_value 
     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.'"'; 

編輯2: 看起來你正在使用這個查詢的框架。我真的建議使用一些參數綁定,而不是直接將值放入查詢中。

+0

這可能是可能的。我會檢查你的想法。 Thx回覆, –

+0

@WalterWhite,如果它解決了您的問題,請記住接受Mikel的回答。 – SpencerD

+0

@Mikel Bitson你睜開了眼睛。問題解決了,我更新了這篇文章。謝謝。 –

相關問題