MySQL 选择所有 [第一个字母以 B 开头]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10156965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select all where [first letter starts with B]
提问by user1333327
This is a follow up question to my previous one. I want to write a MYSQL statement that echoes every entry that starts with letter B.
这是我上一个问题的后续问题。我想写一个 MYSQL 语句来回显以字母 B 开头的每个条目。
Function.php
函数.php
function getCategory() {
$query = mysql_query("SELECT author FROM lyrics WHERE author [starts with letter B]") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) { ?>
<p><a href="##"><?= $row['author']; ?></a></p>
<?php }
Category.php?category=b
Category.php?category=b
<?php include 'includes/header.php' ?>
<?php getCategory(); ?>
<?php include 'includes/footer.php' ?>
Like that I guess. And then one for every letter of the alphabet, and one with misc (numbers etc)
我猜是这样的。然后是字母表中的每个字母一个,一个带有其他(数字等)
回答by ceejayoz
SELECT author FROM lyrics WHERE author LIKE 'B%';
Make sure you have an index on author
, though!
不过,请确保您有一个索引author
!
回答by Bhanu Joshi
This will work for MYSQL
这将适用于 MYSQL
SELECT Name FROM Employees WHERE Name REGEXP '^[B].*$'
In this REGEXP stands for regular expression
在这个 REGEXP 代表正则表达式
and
和
this is for T-SQL
这是用于 T-SQL
SELECT Name FROM Employees WHERE Name LIKE '[B]%'
回答by Waseem Ahmad
SQL Statement:
SQL语句:
SELECT * FROM employee WHERE employeeName LIKE 'A%';
Result:
结果:
Number of Records: 4
employeeID employeeName employeeName Address City PostalCode Country
1 Alam Wipro Delhi Delhi 11005 India
2 Aditya Wipro Delhi Delhi 11005 India
3 Alok HCL Delhi Delhi 11005 India
4 Ashok IBM Delhi Delhi 11005 India
回答by sqluck
You can use:
您可以使用:
WHERE LEFT (name_field, 1) = 'B';
回答by Taz
Following your comment posted to ceejayoz's answer, two things are messed up a litte:
在您对 ceejayoz 的回答发表评论之后,有两件事被搞砸了:
$first
is not an array, it's a string. Replace$first = $first[0] . "%"
by$first .= "%"
. Just for simplicity. (PHP string operators)The string being compared with
LIKE
operator should be quoted. ReplaceLIKE ".$first."")
byLIKE '".$first."'")
. (MySQL String Comparison Functions)
$first
不是数组,而是字符串。替换$first = $first[0] . "%"
为$first .= "%"
。只是为了简单。(PHP 字符串运算符)与
LIKE
运算符比较的字符串应该被引用。替换LIKE ".$first."")
为LIKE '".$first."'")
。(MySQL 字符串比较函数)