我有一个以下SQL查询,如果输入了战斗机的名字或姓氏,则返回结果。如果还输入了名字和姓氏,我需要添加到此SQL查询以返回结果。这是查询:
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 :(得分:2)
在最后一行中,只需更改:
其中A.fighter_lastName喜欢'$ {searchT}%'或A.fighter_firstName喜欢'$ {searchT}%'
使用AND而不是OR
答案 1 :(得分:1)
修改上面的第三行,使其中包含AND:
(SELECT COUNT(fighterId) FROM tblFighter where fighter_lastName like '${searchT}%' AND fighter_firstName like '${searchT}%' ) totalresults,
那应该可以解决你的问题。
编辑: 如果要修改查询以在一个字段“John Doe”中接受两个名称(因此您将搜索John的名字和Doe的姓氏),您将需要在该空间上拆分字符串并执行不同的WHERE子句。您可以尝试在现有WHERE子句中使用某些逻辑,也可以调用其他查询。但是,正如已经指出的那样,你在SQL语句中做了太多的业务逻辑。尝试将此处理带入代码的业务层。例如,您可以在代码中更容易地确定一个名称与您描述的两个名称相对应。然后你可以相应地修改一下这个电话。