2011-05-06 59 views
-4

我有一個下面的SQL查詢,如果輸入了戰鬥機的名字或姓氏,那麼返回結果。如果還輸入了名字和姓氏,我需要添加到此SQL查詢以返回結果。下面是該查詢:如何向SQL查詢中的WHERE子句添加附加條件?

SELECT distinct 
'${searchT}' searchalp, 
(SELECT COUNT(fighterId) FROM tblFighter where fighter_lastName like '${searchT}%' or fighter_firstName like '${searchT}%' ) totalresults, 
A.FighterID, 
A.Fighter_FirstName, 
A.Fighter_NickName, 
if (A.Fighter_Height > 0, 
CONCAT(FLOOR(A.Fighter_Height/12), '\' ', MOD(A.Fighter_Height,12), '"'), '') Fighter_Height_inch, 
A.Fighter_NormalWeight, 
if (A.Fighter_Reach > 0, CONCAT(ROUND(A.Fighter_Reach,0), '"') , '') Fighter_Reach, 
A.Fighter_Stance, 
CONCAT('/fighter/',REPLACE(REPLACE(LCASE(A.Fighter_FirstName),' ','-'),'.',''),'/',REPLACE(LCASE(A.Fighter_LastName),' ','-'),'/',A.FighterID,'/') fighterURL, 
(select count(*) from tblFight_Fighter where FighterID = A.FighterID and OutcomeID = 1) fighterCareerWins, (select count(*) from tblFight_Fighter where FighterID = A.FighterID and OutcomeID = 2) fighterCareerLosses, (select count(*) from tblFight_Fighter where FighterID = A.FighterID and OutcomeID = 3) fighterCareerDraws, 
fighter_lastName, FA.AccoladeDescription FROM tblFighter as A LEFT JOIN (SELECT FighterId, AccoladeDescription, AccoladeType 
    FROM tblAccolade 
    JOIN tblEvent USING(EventID) 
    JOIN (SELECT AccoladeID, Max(Event_Date) as MaxAccDate 
      FROM tblAccolade 
      JOIN tblEvent ON tblAccolade.EventID = tblEvent.EventID 
     WHERE AccoladeType = 'Belt' AND AccoladeDescription not like 'WEC%' 
     GROUP BY AccoladeID) as tmpMaxAccDate ON tblAccolade.AccoladeID = tmpMaxAccDate.AccoladeID 
WHERE Event_Date = MaxAccDate) as FA ON FA.FighterId = A.FighterId and FA.AccoladeType = 'Belt' where A.fighter_lastName like '${searchT}%' or A.fighter_firstName like '${searchT}%' order by A.fighter_lastName ASC LIMIT ${stInd}, ${lstInd} 

下面是當輸入到輸入框中姓什麼是返回一個例子:

<table class="wide"> 
<thead> 
<tr> 
<th>FIRST</th> 
<th>LAST</th> 
<th>NICKNAME</th> 

<th>Ht.</th> 
<th>Wt.</th> 
<th>Reach</th> 
<th>Stnc.</th> 
<th>W</th> 
<th>L</th> 
<th>D</th> 
<th>Belt</th> 
</tr> 

</thead> 
<tbody><tr> 
<td><a href="/fighter/scott/smith/376/">Scott</a></td> 
<td><a href="/fighter/scott/smith/376/">Smith</a></td> 
<td><a href="/fighter/scott/smith/376/">Hands of Steel</a></td> 
<td>6' 0"</td> 
<td>185 lbs</td> 
<td>70"</td> 
<td>Orthodox</td> 
<td>4</td> 

<td>6</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/dillon/smith/1406/">Dillon</a></td> 
<td><a href="/fighter/dillon/smith/1406/">Smith</a></td> 
<td><a href="/fighter/dillon/smith/1406/"></a></td> 
<td>6' 0"</td> 
<td>170 lbs</td> 
<td></td> 
<td></td> 

<td>1</td> 
<td>0</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/maurice/smith/19/">Maurice</a></td> 
<td><a href="/fighter/maurice/smith/19/">Smith</a></td> 
<td><a href="/fighter/maurice/smith/19/"></a></td> 
<td>6' 2"</td> 
<td>221 lbs</td> 

<td></td> 
<td>Orthodox</td> 
<td>6</td> 
<td>3</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/david/smith/1236/">David</a></td> 
<td><a href="/fighter/david/smith/1236/">Smith</a></td> 
<td><a href="/fighter/david/smith/1236/"></a></td> 
<td></td> 

<td>155 lbs</td> 
<td></td> 
<td></td> 
<td>2</td> 
<td>1</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/eric/smith/1027/">Eric</a></td> 
<td><a href="/fighter/eric/smith/1027/">Smith</a></td> 
<td><a href="/fighter/eric/smith/1027/"></a></td> 

<td></td> 
<td>0 lbs</td> 
<td></td> 
<td>Southpaw</td> 
<td>0</td> 
<td>1</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/patrick/smith/48/">Patrick</a></td> 
<td><a href="/fighter/patrick/smith/48/">Smith</a></td> 

<td><a href="/fighter/patrick/smith/48/"></a></td> 
<td>6' 2"</td> 
<td>225 lbs</td> 
<td></td> 
<td>Orthodox</td> 
<td>4</td> 
<td>2</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/adam/smith/1269/">Adam</a></td> 

<td><a href="/fighter/adam/smith/1269/">Smith</a></td> 
<td><a href="/fighter/adam/smith/1269/"></a></td> 
<td></td> 
<td>0 lbs</td> 
<td></td> 
<td></td> 
<td>0</td> 
<td>1</td> 
<td>0</td> 
<td style="width:100px;"></td> 
</tr><tr> 
<td><a href="/fighter/josh/smith/1121/">Josh</a></td> 

<td><a href="/fighter/josh/smith/1121/">Smith</a></td> 
<td><a href="/fighter/josh/smith/1121/"></a></td> 
<td>6' 0"</td> 
<td>155 lbs</td> 
<td></td> 
<td>Orthodox</td> 
<td>0</td> 
<td>1</td> 
<td>0</td> 
<td style="width:100px;"></td> 

</tr></tbody> 
</table>  
+0

這是什麼類型的數據庫? – octopusgrabbus 2011-05-06 14:32:35

+0

這是一個MySQL數據庫 – 2011-05-06 14:34:17

+2

@octopusgrabbus「設計不好」的類型。該查詢是一團糟。 – 2011-05-06 14:34:50

回答

1

在最後一行簡單的改變:

哪裏A.fighter_lastName like'$ {searchT}%'或A.fighter_firstName like'$ {searchT}%'

要用AND代替OR

+0

不起作用 – 2011-05-06 14:38:36

1

修改一行三人以上的有一個,並在其中:

(SELECT COUNT(fighterId) FROM tblFighter where fighter_lastName like '${searchT}%' AND fighter_firstName like '${searchT}%' ) totalresults, 

這應該可以解決你的問題。

編輯: 如果要修改您的查詢,以接受一個字段「李四」兩個名字(因此你會尋找約翰的名字和身份不明的姓氏),您將需要拆分字符串在空間上並執行不同的WHERE子句。您可以嘗試在現有的WHERE子句中輸入一些邏輯,或者可以調用不同的查詢。然而,正如已經指出的那樣,你在SQL語句中做了太多的業務邏輯。嘗試將此處理帶入您的代碼的業務層。例如,您所描述的名稱和兩個名稱可以在您的代碼中更容易地確定。然後你可以相應地修改呼叫。

+0

這不起作用 – 2011-05-06 14:38:20

+4

@ Mojaray2k - 你有沒有考慮用什麼方式描述*這個建議不起作用?它是否改變了你收到的結果?如果是這樣,它以何種方式改善了結果,以及它以何種方式有害?你是唯一可以運行這些查詢的人,所以只要把「不起作用」,不會幫助任何人改正他們的答案。或者,發佈表格定義,樣本數據,當前結果和預期結果,以便其他人*可*運行他們的查詢並測試他們,然後將他們發佈爲答案。 – 2011-05-06 14:47:48

+0

舉起。我只是問的是。我如何做到這一點,如果用戶想要返回查詢的結果,如果輸入了名字和姓氏。現在,如果有名字或姓氏,則返回查詢結果。這是如此令人困惑? – 2011-05-06 14:56:33

相關問題