我有一個下面的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>
這是什麼類型的數據庫? – octopusgrabbus 2011-05-06 14:32:35
這是一個MySQL數據庫 – 2011-05-06 14:34:17
@octopusgrabbus「設計不好」的類型。該查詢是一團糟。 – 2011-05-06 14:34:50