使用MeekroDB查询带有限制的MySQL字母数字

时间:2014-06-02 17:14:01

标签: php mysql meekro

我试图按照某些字母数字范围1-3& amp;前15场比赛的A-B,但输出没有任何结果。使用MeekroDB。不知道如何将我在这里的内容翻译成他们的方法。

$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet
FROM gallery ORDER BY (CASE theme
  WHEN '1%' THEN 1
  WHEN '2%' THEN 2
  WHEN '3%' THEN 3
  WHEN 'A%' THEN 4
  WHEN 'B%' THEN 5
  ELSE -1
END) LIMIT 15"); 

$x = 0
foreach ($results as $row) {
  $x++;
  if ($x == 1) {  // first in query
    $t1 = $row['theme'];
    $d1 = $row['developer'];
    $th1 = $row['thumb'];
    $thlg1 = $row['thumb_lg'];
  }
  ...
}

身体中的回声示例:

<img src="<?php echo($th1); ?>" data-retina="<?php echo($thlg1); ?>" alt="<?php echo($t1); ?>" />
<span><p class="hname"><?php echo($t1); ?></p>
<p class="hdev"><?php echo($d1); ?></p></span>

UPDATE2:

$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg
FROM gallery ORDER BY (CASE
  WHEN theme LIKE '1%' THEN 1
  WHEN theme LIKE '2%' THEN 2
  WHEN theme LIKE '3%' THEN 3
  WHEN theme LIKE 'A%' THEN 4
  WHEN theme LIKE 'B%' THEN 5
  ELSE -1
END) LIMIT 15");

UPDATE3:

$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery
  ORDER BY (CASE Alphabet
    WHEN '1' THEN 1
    WHEN '2' THEN 2
    WHEN '3' THEN 3  
    WHEN 'A' THEN 4
    WHEN 'B' THEN 5
    ELSE 6
  END)
  LIMIT 15");

1 个答案:

答案 0 :(得分:1)

ORDER BY子句应为:

ORDER BY CASE Alphabet
    WHEN '1' THEN 1
    WHEN '2' THEN 2
    WHEN '3' THEN 3
    WHEN 'A' THEN 4
    WHEN 'B' THEN 5
    ELSE 6
END, theme

您的代码在整个主题上进行完全匹配,&#39; 1%&#39;应该使用LIKE进行匹配。我的版本只使用了您已经提取到Alphabet的第一个字符。

另一种写这个的方法是:

ORDER BY IF(LOCATE(Alphabet, "123AB"), 0, 1), theme

这是有效的,因为您的第一个字符的顺序与正常的字典顺序匹配。