MYSQL select where column like column1%column2 - mysql

I have a table with 3 fields, name, firstname and lastname
I want to see how many rows in the table have name of the form firstname%lastname
I tried to do
select * from family_watchdog_offender where name like firstname%lastname\G
but that returned a syntax error regarding the %lastname portion of the query. Is there some syntax that will allow me to run a query such as this?

SELECT * FROM family_watchdog_offender WHERE name LIKE CONCAT(firstname, '%', lastname);

Try concat-ing the %:
select * from family_watchdog_offender where name like CONCAT(firstname, '%', lastname)

I don't think you can do that...This may be what you need to do
SELECT *
FROM family_watchdog_offender
WHERE name LIKE CONCAT(firstname, '%')
AND name LIKE CONCAT('%', lastname);

Related

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.

SQL search in multitable

i have two tables: songs and singers
and this my query:
Select * FROM (
SELECT
idx,
name,
IDsinger,
permission,
(name LIKE '%XXX%') As relevancy,
'table1' As t
FROM `songs`
where
isActive<>'0' AND name LIKE '%XXX%'
UNION
SELECT
idx,
name,
CreationDate,
permission,
(name LIKE '%XXX%') As relevancy,
'table2' As t
FROM `singers`
WHERE isActive<>'0' AND name LIKE '%XXX%'
) AS X
order by relevancy LIMIT 10
The problem is if i write "akon lonely" is not found result.
But if i write "akon" or "lonely" is found result.
And i would love suggestions for improving query..
Thanks
Your result is correct. AKON is a singer and Lonely is the song. Neither the Song or the Singer is called Akon lonely.
Sounds like you are trying to do a more complicated form of searching like contains.
http://technet.microsoft.com/en-us/library/ms187787.aspx
You could try
name like '%' + #xxx + '%' or #xxx like '%' + name + '%'

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 - select concat query

I'm trying to list all users beginning with a letter, e.g. D
Would the following be the right method of doing this.
Select concat(firstname, '',lastname) from users where concat(lastname) = "D*"
SELECT concat(firstname, '',lastname) FROM users WHERE lastname LIKE "D%"
If you want to use wildcards, you need the LIKE operator. Also, in your where clause you only have one column (lastname), so you don't need concat.
i would try:
select * from users where lastname like 'D%';
For getting list starting with eg: "D":
SELECT firstname FROM users WHERE LEFT(firstname,1)= 'D';

Compare value with concatenated fields in where clause

Say I want to search for a user, 'Richard Best'. Is it possible to compare the full name is concatenated first name and last name? I do not have a full name field.
select * from users where last_name + ' ' + first_name like '%richa%'
I am using Mysql
These are equivalent:
select * from users where concat(last_name,' ',first_name) like '%richa%'
select * from users where concat_ws(' ',last_name,first_name) like '%richa%'
This might also work:
select * from users where last_name like '%richa%' or first_name like '%richa%'
Take a look at this thread.
Using mysql concat() in WHERE clause?
select * from users where (first_name + ' ' + last_name) like '%richa%'
In laravel Eloquent you can use whereRaw("concat(first_name,' ',last_name) LIKE %$search%")