I am trying to make a MySQL query where I filter the records based on the search text using LIKE keyword.
For example if the user searches for Illusion Softwares where Illusion is First name and Softwares is last name so the query should search for columns FirstName, LastName and concat both and search.
I have tried this so far but it does not work for CONCAT
Select Contacts.*, Contacts.ID as CID from Contacts left join
website_Data on Contacts.ID = website_Data.ContactID where
Contacts.`FirstName` LIKE '%Illusion Softwares%' or
Contacts.`LastName` LIKE '%Illusion Softwares%' or
concat(Contacts.FirstName, ' ', Contacts.LastName)
LIKE '%Illusion Softwares%'
or Contacts.Email LIKE '%Illusion Softwares%'
order by Contacts.`Created` DESC
What am I missing? Please help me.
Your query should be like this
Select Contacts.*, Contacts.ID as CID
from Contacts
left join website_Data
on Contacts.ID = website_Data.ContactID
where CONCAT(Contacts.FirstName,' ', Contacts.LastName) LIKE '%Illusion Softwares%'
order by Contacts.`Created` DESC
SELECT *,c.ID as CID
FROM Contacts c
LEFT OUTER JOIN website_Data w ON c.ID=w.ContactID
WHERE c.Email OR CONCAT_WS(' ',c.FirstName,c.LastName) LIKE '%Illusion Softwares%'
ORDER BY c.Created DESC;
you can try this also, this worked for me,
$this->db->or_like('CONCAT(Contacts.FirstName,' ', Contacts.LastName)',$search);
Use CONCAT_WS in your query:
$this->db->or_like('CONCAT_WS(Contacts.FirstName,' ', Contacts.LastName)',$search);
Related
I am trying to search for query on first name and last name field
here is my query
SELECT d.title, GROUP_CONCAT(u.firstName,' ',u.lastName) as fullname
FROM DEALS d
left JOIN USER u ON u.idUser = d.userId
WHERE ((d.title LIKE '%goutham%' OR d.keywords LIKE '%goutham%')
OR fullname LIKE '%goutham%') AND d.isPublic=1
But i got
Unknown column 'fullname' in 'where clause'
The immediate cause of your problem is that you can't refer to an alias defined in the SELECT clause in the WHERE clause of the same query. The solution would be to repeat the entire expression instead of using the alias. However, based on your comment, you really want to check the first name, so do just that:
SELECT
d.title,
CONCAT(u.firstName, ' ', u.lastName) AS fullname
FROM DEALS d
LEFT JOIN USER u
ON u.idUser = d.userId
WHERE
(d.title LIKE '%goutham%' OR
d.keywords LIKE '%goutham%' OR
u.firstName LIKE '%goutham%') AND d.isPublic = 1;
You cannot use a column alias in where. It has nothing to do with the rest of your query.
You don't have a GROUP BY, so I suspect GROUP_CONCAT() is not intended. Instead you want CONCAT(). You can repeat the expression in the WHERE, but I think you should look in each component:
SELECT d.title, CONCAT(u.firstName, ' ', u.lastName) as fullname
FROM DEALS d left JOIN USER
u
ON u.idUser = d.userId
WHERE (d.title LIKE '%goutham%' OR
d.keywords LIKE '%goutham%' OR
u.firstName LIKE '%goutham%' OR
u.lastName LIKE '%goutham%'
) AND
d.isPublic = 1;
If you care about performance and are looking for words, then you might want to look into MySQL's full text index capabilities.
If you still want to look on the combination, I would recommend repeating the expression:
WHERE (d.title LIKE '%goutham%' OR
d.keywords LIKE '%goutham%' OR
CONCAT(u.firstName, ' ', u.lastName) LIKE '%goutham%'
) AND
d.isPublic = 1;
I have this data in my database(3,15,6,4,15), and I tried to show it on my table using group concat, the problem is i got duplicate results. (3,15,6,4,15,3,15,6,4,15,3,15,6,4,15).
I tried to use distinct but it eliminate also the other "15".
What is the best solution for that?
thanks!
this is my query
SELECT users.*, GROUP_CONCAT(written.score separator ' - ') as Wscore, student_subject.*,SUM(written.score) as total, SUM(written.item) as item FROM users JOIN written ON users.idnumber=written.idnumber JOIN student_subject ON users.idnumber=student_subject.idnumber WHERE student_subject.teacher='$login_session' AND written.section='$section' AND written.level='$level' AND written.year='$year' AND written.subject='$subject' AND users.gender='male' AND written.period='first' GROUP BY users.idnumber order by users.lname
You probably just want distinct in the group_concat():
SELECT u.*, GROUP_CONCAT(distinct w.score separator ' - ') as Wscore,
ss.*, SUM(w.score) as total, SUM(w.item) as item
FROM users u JOIN
written w
ON u.idnumber = w.idnumber JOIN
student_subject ss
ON u.idnumber = ss.idnumber
WHERE ss.teacher = '$login_session' AND w.section='$section' AND
w.level = '$level' AND w.year = '$year' AND w.subject = '$subject' AND
u.gender = 'male' AND w.period = 'first'
GROUP BY u.idnumber
order by u.lname;
Notice how table aliases make the query easier to write and to read.
I have produced the following query.
SELECT t.id AS playerid,
dp.first_name,
dp.surname
FROM ".TBL_FOOT_CAREER_TEAMS." t
INNER JOIN ".TBL_FOOT_CAREER_DB_PLAYERS." dp
ON dp.id = t.playerid
WHERE t.careerid = '$career'
AND (dp.first_name LIKE '%{$keyword[$i]}%')
OR (dp.surname LIKE '%{$keyword[$i]}%')
OR (`dp.first_name + dp.surname` LIKE '%{$keyword[$i]}%')
There are two columns in the database. first_name and surname. As you can see, I'm trying to check if the keyword is in either of those columns. I also try and make them into one complete name and check if that's what the search term is aswell.
I'm getting an error so I can assume this isn't the way to do it!!
Can someone help :)
Thanks
SELECT t.id AS playerid,
dp.first_name,
dp.surname
FROM ".TBL_FOOT_CAREER_TEAMS." t
INNER JOIN ".TBL_FOOT_CAREER_DB_PLAYERS." dp
ON dp.id = t.playerid
WHERE (t.careerid = '$career') AND
(
(dp.first_name LIKE concat('%', $keyword[$i], '%')) OR
(dp.surname LIKE concat('%', $keyword[$i], '%')) OR
(CONCAT(dp.first_name, ' ',dp.surname) LIKE concat('%', $keyword[$i], '%'))
)
UPDATE
since you are concactenating the name, you can it like this:
SELECT t.id AS playerid,
dp.first_name,
dp.surname
FROM ".TBL_FOOT_CAREER_TEAMS." t
INNER JOIN ".TBL_FOOT_CAREER_DB_PLAYERS." dp
ON dp.id = t.playerid
WHERE (t.careerid = '$career') AND
(
CONCAT(dp.first_name, ' ',dp.surname) LIKE concat('%', $keyword[$i], '%')
)
Use CONCAT() in your query: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
...
OR CONCAT(dp.first_name, dp.surname) LIKE '%{$keyword[$i]}%'
I whant to do something like this:
SELECT
s.name,
COUNT(r.request_log_id) AS NumberOfRequests
FROM
station AS s
LEFT JOIN request_log AS r ON r.requested LIKE '%s.station_id%'
GROUP BY 1
But this gives 0 in the NumberOfRequests and i think it is becouse its hardcoded s.station_id its running LIKE on.
Does anyone know how i can do this?
For your information, r.requested is a string whit some data in it and one example is
/StationInfo - stationID = "262" - Status = 0
So it is the staitonid i whant to get LIKE on and cound how many rows in request_log table it is in that have the station id for the first table named station
First approach:
SELECT
s.name,
COUNT(r.request_log_id) AS NumberOfRequests
FROM
station AS s
LEFT JOIN request_log AS r
ON r.requested LIKE concat( '%',s.station_id, '%')
GROUP BY 1
be careful with this non equijoin.
see you.
I am trying to convert this MSSQL QUERY to MYSQL
Query looks like this
select tu.FirstName+' '+tu.LastName as name,tg.Name as game_name,tg.Status,tg.UserId,tg.gameid from tblUsers tu
inner join tblGame tg on
tu.UserId=tg.UserId where tg.Name LIKE #Name + '%'
the same query doesnt return any records when i run it on MYSQL, what is the issue ? It works good on SQL server
SELECT CONCAT_WS(' ', tu.FirstName, tu.LastName) AS name, tg.Name AS game_name,tg.Status,tg.UserId,tg.gameid
FROM tblUsers tu
INNER JOIN tblGame tg ON tu.UserId=tg.UserId
WHERE tg.Name LIKE CONCAT(tu.FirstName, ' ', tu.LastName, '%')
Notes:
it's case insensitive, I've used upper case just for readability
I've intentionally used both concat and concat_ws to show them as options
select tu.FirstName+' '+tu.LastName as name,tg.Name as
game_name,tg.Status,tg.UserId,tg.gameid from tblUsers tu
inner join tblGame tg on
tu.UserId=tg.UserId where tg.Name LIKE #Name + '%'
select concat(tu.firstName,' ',tu.lastName) as name, ,tg.Name as
game_name,tg.Status,tg.UserId,tg.gameid from tblUsers as tu
inner join tblGame as tg on
tu.UserId=tg.UserId where tg.Name LIKE concat(#Name,'%');
Note that, there is not (for example) field1+' '+field2,
There is concat, concat_ws, and group_concat,
read about it, it's super useful.