SQL Select Statement LIKE 'T%' - mysql

I am trying to run a SELECT Query that pulls the Last Names that begin with T. When I run it I just get 0 results but there are names in the table that do begin with the letter T.
What am I doing wrong?
Here is my Code
SELECT FirstName, LastName, City
FROM Customers
WHERE LastName LIKE 'T%';

The most likely cause for this is that you have leading spaces in LastName column values.
To find all entries that have at least one leading space
SELECT FirstName, LastName, City
FROM Customers
WHERE LastName LIKE ' %';
Here is SQLFiddle demo
To fix such entries you can do
UPDATE Customers
SET LastName = TRIM(LastName)
WHERE LastName LIKE ' %';
Here is SQLFiddle demo

Related

mysql query to remove space after last comma in string

I have a mysql table with data like this.
Fullname
Adarna , Neil.
David , Jundemil.
I want to remove all spaces between lastname and the comma
so the data looks like this.
Adarna, Neil.
David, Jundemil.
tried this query
SELECT SUBSTRING_INDEX(`fullname`, ' ,', 2) from tbl_users
output was all fullname with commas
You can try as per below-
update mytable set fullname = replace(fullname,' ,',',');

Concat 2 columns in a full table view

here is an example of what my problem is
CustomerID FirstName LastName FullName
1 Ken Weger Ken Weger
this is my output what i need to do is to replace the FirstName and LastName columns with FullName using Concat in a select statement. The code I am getting to create this result looks like this
Select *,
Concat (firstname, ' ', lastname) as fullname from customer;
Please let me know what i am doing wrong and how to fix it.
SELECT CustomerID, CONCAT(FirstName, ' ', LastName) AS FullName FROM Table
select * selects all columns from table.

search box -> I need to concat first name and last name from one table and join it to a 2nd and return item from 2nd table

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%'

MYSQL: Is it possible to concatenation multiple rows of record into one row

I want to know if there is any option to output multiple rows into a single row.
for example the regular select * from tbl_name will give a list of all records available in the table.
firstname lastname
---------- ------------
Lepanto Fernando
Lourdes Brillianto
Gerald Siluvai
Preferred output
firstname will have -> Lepanto###Lourdes###Gerald
lastname will have -> Fernando###Brillianto###Siluvai
Can we have some concatenation done to achieve the above.
Use GROUP_CONCAT()
select group_concat(firstname separator '###') as firstnames,
group_concat(lastname separator '###') as lastnames
from your_table
Use:
select GROUP_CONCAT(firstname SEPARATOR "###") as firstname,
GROUP_CONCAT(lastname SEPARATOR "###") as lastname
from tblname

Sql query returns only one row (LIKE)

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.