My table name is student and column name is FullName.
Can anyone help with this question? I have tried:
select FullName from student where fullName like "e"
But this is returning 0 rows.
If you want students that contain at leas one 'e', then:
select fullname
from student
where fullname like '%e%'
Note the use of the wildcard character (%) around the e, which searches for the character anywhere in the string.
But if you really mean students that contain a single e (not more, not less), then you need to filter out names that contain more than one. For this, you can do:
select fullname
from student
where fullname like '%e%' and fullname not like '%e%e%'
You could also use replace() and char_length():
select fullname
from student
where char_length(replace(fullname, 'e', '')) = char_length(fullname) - 1
You can use a regular expression:
select fullname
from student
where fullname regexp '^[^e]*e[^e]*$'
Related
Names:
Watts, Odysseus D.
Schultz, Wilma D.
Mckinney, Madaline R.
Charles, Mohammad O.
My table names like this structure, first_name and last_name are in single column called names, i want to get all names from starting with letter 'M' in their last name,
How to write in mysql query for this criteria???
If the last name appears at the beginning of the string, the you can just do:
select * from names where name like 'M%'
On the other hand, if you want to filter on the first character after the comma, then:
select * from names where name like '%, M%'
writing a select statement to view first names with spaces in them i.e. JO ANN or TERRY LYNN,
my statement format would look like:
SELECT FirstName FROM `DB`.`TABLE` where FirstName REGEXP ' '
I know the names exist because I can see them in the preview i just need to write a select statement to only view the names with spaces
It's better to use POSIX class which matches all the space characters because your's won't match the names which has tabs.
SELECT FirstName FROM `DB`.`TABLE` where FirstName REGEXP '[[:space:]]'
This would match the names only if there exists a space between two alphabets.
SELECT FirstName FROM `DB`.`TABLE` where FirstName REGEXP '[[:alpha:]][[:blank:]]+[[:alpha:]]'
I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.
So the search would be like 'Ozzie Smith'
First table has (username, fname, lname) = osmith, ozzie, smith
Second table has (username, team) = osmith, cardinals
I need to concat the first and last name columns from the first table join it by username on the second table and return the team name.
I have been trying and trying...brain melted. I need your help!
Thanks!
Since it's MySQL, you need to use CONCAT, not the + sign. I also added a LOWER() function to avoid capital letter mismatch problem :
select team
from table1
join table2 on table2.username = table1.username
where lower(concat(fname,' ',lname)) like lower('%Ozzie Smith%')
You're probably doing something like
WHERE fname LIKE '%Ozzie Smith%'
which will not work. fname will contain only Ozzie, and will never match against Ozzie Smith. It would also not match if the user enters Smith, Ozzie, or any other dual-name variant. You'll have to preprocess the search terms and do something like
WHERE (fname LIKE '%Ozzie%') or (fname LIKE '%Smith%')
OR (lname LIKE '%ozzie%') or (lname LIKE %Smith%')
This will get VERY painful as the number of search terms and fields being search goes up. You'd be better off using a FULLTEXT index, where it'll be a simple matter of
WHERE MATCH(fname, lname) AGAINST ('Ozzie Smith')
Why doesn't this work?
select
lname + ', ' + fname as playerName
, team as teamName
from table1
join table2 on table2.username = table1.username
Update:
where (fname + ' ' + lname) like '%Ozzie Smith%'
SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.