sql:按名称排列两个表

kuhbmx9i  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(275)

这个问题在这里已经有答案了

mysql按2列排序(2个答案)
两年前关门了。
有什么方法可以让我改变这个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>";

这样,这个图像怪物数据库的名称和类型是按类型排序的,但是与该类型相关联的怪物名称也是按字母顺序排列的?所以对于野兽类型,它会被列为

Boar         Beast

Grizzly Bear Beast

Panther      Beast

Polar Bear   Beast

Titanoboa    Beast

Wolf         Beast

谢谢您!
编辑:解决方案是monstertype.name asc,mname asc。谢谢大家!

cuxqih21

cuxqih211#

这应该起作用:

$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";
ovfsdjhp

ovfsdjhp2#

更改您的order by语句以在怪物类型后包含怪物名称。

$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";

相关问题