2011-05-17 86 views
1

我想弄清楚如何在我的程序中使用substr從表中獲取適當的數據。我如何使用PHP substr?

用戶摘機 '快報' 「AA」, 「AB」, 「AC」, 「AD」, 「AE」 或 「全部」

然後,我需要,如果 'panelID' 比較有「快報'我假設使用substr。

panelID例如:1122AA-0010

$_SESSION['yield'] = " 
from (select distinct panelID from DATABASE.TABLE) s, DATABASE.TABLE p 
where p.panelID = s.panelID and p.laminationts > '$start_date' and p.laminationts < '$end_date' 
group by week(p.laminationts)"; 

像這樣的事情

"where substr(p.panelID,4,2) = substr(s.panelID,4,2) and p.laminationts > '$start_date' and p.laminationts < ..." 
+0

凡在查詢中實現你的用戶輸入? – mellamokb 2011-05-17 03:50:28

+0

你想使用mysql的['substring'](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring)或php的['substr'](http:/ /php.net/manual/en/function.substr.php)? – scurker 2011-05-17 03:52:27

+0

用戶輸入尚未實現。我想使用php的子串。我是全新的。 – TheRealDK 2011-05-17 05:23:14

回答

0

你需要SUBSTR(p.panelID,5,2),因爲第一個字符的位置是1,不0.

另外,請注意,在WHERE子句中使用函數會避免在這些字段上使用任何索引。也許你可以重構你的數據庫(例如在多個字段中拆分panelID並在這些字段上創建一個複合PK),以允許使用索引