Select all where [first letter starts with B]
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.
Function.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
<?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)
SELECT author FROM lyrics WHERE author LIKE 'B%';
Make sure you have an index on author
, though!
This will work for MYSQL
SELECT Name FROM Employees WHERE Name REGEXP '^[B].*$'
In this REGEXP stands for regular expression
and
this is for T-SQL
SELECT Name FROM Employees WHERE Name LIKE '[B]%'
SQL Statement:
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
You can use:
WHERE LEFT (name_field, 1) = 'B';
Following your comment posted to ceejayoz's answer, two things are messed up a litte:
$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)
ReferenceURL : https://stackoverflow.com/questions/10156965/select-all-where-first-letter-starts-with-b
'programing' 카테고리의 다른 글
Create a HTML table where each TR is a FORM (0) | 2021.01.17 |
---|---|
import win32api error in Python 2.6 (0) | 2021.01.17 |
What are good alternatives to UITextAlignmentCenter in iOS 6? (0) | 2021.01.17 |
Using RSpec to check if something is an instance of another object (0) | 2021.01.17 |
How to set include path in xcode project (0) | 2021.01.17 |