2012-03-28 103 views
1

我的服務器數據庫花了太長時間才能通過單獨的線程運行兩個sql查詢。我這樣做在PHP中:一個SQL查詢而不是三個sql查詢

$First=mysql_query("SELECT `ID`,`Country` FROM `Users` WHERE `IP`='".$ip."'",$connection); 

$row=mysql_fetch_assoc($First); 
$id=$row['ID']; 
$ucountry=$row['Country']; 

$opt=mysql_query("SELECT Size,Paper,Zip FROM Tools WHERE (Users LIKE '%".$id."%') OR (Users LIKE '".$id."%') OR (Users LIKE '%".$id."') OR (Users LIKE '".$id."') OR (Users LIKE 'All users') OR (Country LIKE '".$ucountry."') OR (Country LIKE 'All countries')",$connection); 

如何在只有一個查詢在mysql中做所有這些東西?

我嘗試這樣做:

SELECT (SELECT `ID` FROM `Users` WHERE `IP`='xx.xx.xx.xx' ) AS UID, (SELECT `Country` FROM `Users` WHERE `IP`='xx.xx.xx.xx' ) AS UCountry, (SELECT Size,Paper,Zip FROM Tools WHERE (Users LIKE CONCAT('%#',UID,'#%')) OR (Users LIKE CONCAT('#',UID,'#%')) OR (Users LIKE CONCAT('%#',UID,'#')) OR (Users LIKE CONCAT('#',UID,'#')) OR (Users LIKE 'All users') OR (Country LIKE UCountry) OR (Country LIKE 'All countries')) 
+4

通過將其作爲一個查詢,您不一定會使它更快。事實上,它通常變得更慢。 – 2012-03-28 20:00:00

+2

OR(Users LIKE'%「。$ id。」%')OR(Users LIKE'「。$ id。」%')OR(Users LIKE'%「。$ id。」')OR(Users LIKE' 「。$ id。」')或(用戶LIKE'所有用戶')'? – 2012-03-28 20:00:31

+1

爲什麼每個比較都有'LIKE'? '='可能更快。 – 2012-03-28 20:03:43

回答

0

如果您需要確保只有第一行上使用您的選擇從用戶在你需要加入SQL被複制到內嵌查詢

SELECT t.SIZE, 
     t.paper, 
     t.zip 
FROM tools t 
     INNER JOIN (SELECT `id`, 
          `country` 
        FROM `users` 
        WHERE `ip` = ? 
        LIMIT 1) u 
     ON t.Users like CONCAT('%',u.id '%') 
      OR t.user LIKE 'All users' 
      OR Country LIKE CONCAT('%',u.country '%') 
      OR Country LIKE 'All countries' 
+0

這也不錯。謝謝 – Jigberto 2012-03-28 20:31:11

0

你只需要使用通配符等。您的某些LIKE似乎是多餘的,因爲您已經在前面和後面與%進行了匹配。

$query = "SELECT Users.ID 
      ,Users.Country 
      ,Tools.Users 
      ,Tools.Country 
      ,Tools.Size 
      ,Tools.Paper 
      ,Tools.Zip 
FROM Tools 
INNER JOIN Users 
ON Users.IP = '$ip' 
AND (
    Tools.Users LIKE CONCAT('%', Users.ID, '%') 
    OR Tools.Users = 'All users' 
    OR Tools.Country LIKE Users.Country 
    OR Tools.Country = 'All countries' 
)"; 
+0

這也是不錯的....我的問題是,我從來沒有使用內部連接,它確實需要我的棘手部分:) – Jigberto 2012-03-28 20:32:44

0
SELECT t.Size, t.Paper, t.Zip 
FROM Tools AS t 
INNER JOIN Users AS u 
    ON (Users LIKE CONCAT('%', u.ID, '%')) 
    OR (Users LIKE CONCAT(u.ID, '%')) 
    OR (Users LIKE CONCAT('%', u.ID)) 
    OR (Users LIKE u.ID) 
    OR (Users LIKE 'All users') 
    OR (Country LIKE u.Country) 
    OR (Country LIKE 'All countries') 
WHERE (u.IP = '$ip') 

你的一些LIKE標準是多餘的,因爲他們是因爲前一個一定是真的。

因此,前面的查詢等價於:

SELECT t.Size, t.Paper, t.Zip 
FROM Tools AS t 
INNER JOIN Users AS u 
    ON (Users LIKE CONCAT('%', u.ID, '%')) 
    OR (Users LIKE 'All users') 
    OR (Country LIKE u.Country) 
    OR (Country LIKE 'All countries') 
WHERE (u.IP = '$ip') 
+0

完美.... Skrol29最好的:) – Jigberto 2012-03-28 20:29:23

+0

如果我想只插入一行,如果以下SELECT返回一個0或沒有行。 類似於: **如果(選擇是0)則INSERT()** 我該如何在MYSQL中執行此操作? 我只想做一個查詢,而不是這兩個查詢: $ r = mysql_query(「SELECT ID,Country FROM Users WHERE IP ='」。$ ip。「'」,$ connection); ($!r || mysql_num_rows($ r)<= 0) $ ir = mysql_query(「INSERT INTO Users(IP,Country)VALUES('」。$ ip。「','」。$ country 「')」,$連接); – Jigberto 2012-03-28 21:30:28