【问题标题】:Removing non-letter characters from data using SELECT DISTINCT in MySQL在 MySQL 中使用 SELECT DISTINCT 从数据中删除非字母字符
【发布时间】:2019-01-30 08:02:45
【问题描述】:

我有一个包含城市名称数据的表格,例如“(Istanbul)”和“Istanbul”

我试图只显示城市名称,无论是原始形式还是括号中。

为此,我使用以下代码。

<?php
require_once "config.php";
$sql = $conn->query( "SELECT DISTINCT city FROM countries ORDER BY city" );
if ( $sql->num_rows > 0 ) {
// output data of each row
while ( $row = $sql->fetch_assoc() ) {
    echo '<li> <input type="checkbox" name="cityName" value="' . $row[ "city" ] . '" />' . $row[ "city" ] . '</li>';
}
} else {
echo "0 results";
}
?>

我尝试使用 替换(替换(城市,'(',''),')','')

但我无法将其合并到 SELECT DISTINCT。

我不确定这是否可能。

提前致谢!

【问题讨论】:

    标签: php mysql distinct


    【解决方案1】:

    您也可以添加别名以按顺序使用相同的结果

         "SELECT DISTINCT replace(replace(city, '(', ''), ')', '')  as city 
          FROM countries 
          ORDER BY city"
    

    【讨论】:

      【解决方案2】:

      试试这个查询希望它有帮助:-

      $sql = $conn->query( "SELECT DISTINCT city FROM countries ORDER BY city WHERE city LIKE '(%)'" );
      

      $sql = $conn->query( "SELECT DISTINCT city FROM countries ORDER BY city WHERE city LIKE '(%' AND LIKE '_)%'" );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-29
        • 1970-01-01
        • 2019-11-26
        • 2013-04-30
        • 1970-01-01
        • 2014-04-26
        相关资源
        最近更新 更多