我的服務器數據庫花了太長時間才能通過單獨的線程運行兩個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'))
通過將其作爲一個查詢,您不一定會使它更快。事實上,它通常變得更慢。 – 2012-03-28 20:00:00
OR(Users LIKE'%「。$ id。」%')OR(Users LIKE'「。$ id。」%')OR(Users LIKE'%「。$ id。」')OR(Users LIKE' 「。$ id。」')或(用戶LIKE'所有用戶')'? – 2012-03-28 20:00:31
爲什麼每個比較都有'LIKE'? '='可能更快。 – 2012-03-28 20:03:43