2015-02-06 77 views
2

這是一個非常煩人的異常,只發生在phpMyAdmin中,而不是通過php的mysqli或mysql命令行。phpMyAdmin:FROM後忽略默認數據庫

我有兩個數據庫,foobar
foo.bananaTable.monkeyIdbar.monkey.id

因此,一個外鍵,不論你在何處執行它的,下面的SQL將正常工作,因爲-是:

SELECT bananaTable.ripeness as 'ripeness' 
FROM bar.monkey as monkey 
JOIN foo.bananaTable as bananaTable ON bananaTable.monkeyId = monkey.id 

現在,如果你導航到foo數據庫phpMyAdmin,它有效地使用foo作爲你的默認數據庫,如果你沒有明確地在你的SQL中命名一個。因此,下面的SQL(在JOIN離開了foo分貝範圍)應該工作....

SELECT bananaTable.ripeness as 'ripeness' 
FROM bar.monkey as monkey 
JOIN bananaTable as bananaTable ON bananaTable.monkeyId = monkey.id 

而且它在PHP的mysqli並在mysql命令行工作。但它不能在phpMyAdmin中工作你會收到一個錯誤,說#1146 Table "bar.bananaTable" doesn't exist

這是告訴我,FROM完全劫持我在phpMyAdmin中的默認數據庫。事實上,如果您編輯並再次運行它,您會注意到它實際上已將您移動到bar數據庫... WTF?

好了,我想一兩件事:

USE foo; 
SELECT bananaTable.ripeness as 'ripeness' 
FROM bar.monkey as monkey 
JOIN bananaTable as bananaTable ON bananaTable.monkeyId = monkey.id 

...,試圖迫使它。仍然沒有workey!

有沒有人看到這個?這只是一個phpMyAdmin的人工製品,是否太聰明?有沒有一個以此爲中心的配置?或者,這是我需要留意的MySql中的一些奇怪現象嗎?我都是明確的數據庫命名,但出於安全原因,我們從頭開始指定一個默認值。

回答

0

phpMyAdmin版本沒有指定,但它似乎沒有版本相關。以下測試顯示了最新版本的phpMyAdmin中的相同行爲。對我來說看起來像一個bug。

-- http://demo.phpmyadmin.net/master-config 
-- phpMyAdmin Demo Server: Currently running Git revision RELEASE_4_3_0RC2-1597-g6111104 

-- Create table in test database 

CREATE TABLE IF NOT EXISTS bar (
    id int(11) NOT NULL, 
    `name` varchar(20) NOT NULL, 
    fooID int(11) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; 

INSERT INTO bar (id, name, fooID) VALUES 
(1, 'first', 1), 
(2, 'second', 2); 

ALTER TABLE bar 
    ADD PRIMARY KEY (id); 

-- Create table in world database 

CREATE TABLE IF NOT EXISTS `foo` (
    `id` int(11) NOT NULL, 
    `val1` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

INSERT INTO `foo` (`id`, `val1`) VALUES 
(1, 10), 
(2, 15); 

ALTER TABLE `foo` 
    ADD PRIMARY KEY (`id`); 

-- from world database, where table foo resides 

-- this query shows world as the default/selected database, as expected. 

SELECT DATABASE(); 

-- this query should work from world, but fails with #1146 - Table 'test.foo' doesn't exist 

SELECT b.*, f.val1 
FROM test.bar AS b 
LEFT JOIN foo AS f ON f.id = b.fooID 
WHERE 1; 

-- this query succeeds with an explicit world.foo, but it shouldn't be required 

SELECT b.*, f.val1 
FROM test.bar AS b 
LEFT JOIN world.foo AS f ON f.id = b.fooID 
WHERE 1;