SQL:按名称

时间:2018-04-26 19:10:26

标签: php mysql

我有办法改变这个MYSQL语句

$query = "SELECT Monsters.ID AS `mID`, 
          Monsters.Name AS `MName`, Monsters.MonsterType_ID, 
          MonsterType.Name FROM Monsters ";
$query .= "inner join MonsterType ON Monsters.MonsterType_ID = MonsterType.ID 
          ORDER BY MonsterType.Name ASC";
 $result = $mysqli->query($query);

if ($result && $result->num_rows > 0) {
echo "<div class='row'>";
echo "<center>";
echo "<h2>The Monster Database</h2>";
echo "<table>";
echo "<tr><th>Name</th><th>Type</th>
<th></th><th></th></tr>";
while ($row = $result->fetch_assoc())  {
    echo "<tr>";    
    //Output FirstName and LastName
    echo "<td>" .$row["MName"]."</td>";
    echo "<td>" .$row["Name"]."</td>";

这样图像Monster Database Names and Types按类型排序,但与该类型相关联的怪物名称也按字母顺序排列?因此对于野兽类型,它被列为

Boar         Beast

Grizzly Bear Beast

Panther      Beast

Polar Bear   Beast

Titanoboa    Beast

Wolf         Beast

谢谢!

编辑:解决方案是MonsterType.Name ASC,MName ASC。谢谢大家!

2 个答案:

答案 0 :(得分:1)

这应该有效:

$query = "SELECT Monsters.ID AS `mID`, 
  Monsters.Name AS `MName`, Monsters.MonsterType_ID, 
  MonsterType.Name FROM Monsters ";
$query .= "inner join MonsterType ON Monsters.MonsterType_ID = 
  MonsterType.ID ORDER BY MonsterType.Name ASC, MName ASC";

答案 1 :(得分:1)

按语句更改您的订单,以包含怪物类型后的怪物名称。

$query = "SELECT Monsters.ID AS `mID`   , 
          Monsters.Name       AS `MName` , 
          Monsters.MonsterType_ID        , 
          MonsterType.Name FROM Monsters 
        INNER JOIN MonsterType ON Monsters.MonsterType_ID = MonsterType.ID 
          ORDER BY MonsterType.Name,Monsters.Name ASC";