mysql group concat unknow column error - mysql

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;

Related

SQL AND and OR operators behave unexpected

I'm making the below query:
SELECT
`user_organisation`.*,
`users`.`full_name`,
`users`.`email`,
`users`.`avatar`
FROM
`user_organisation`
INNER JOIN `users` ON `users`.`id` = `user_organisation`.`user_id`
WHERE
`users`.`full_name` LIKE '%test%' OR `users`.`email` LIKE '%test%' AND `user_organisation`.`organisation_id` = 111
And I get the results containing user_organisation.organisation_id equals 111 and 222. I want to search records with users.full_name or users.email containing value test. This SQL successfully searches users with full name or email containing string test but not only user_organisation.organisation_id=111.
What the reason for that unexpected behaviour? I sure I'm missing something but I can't see...
You need parentheses in your WHERE clause around the two ORed terms:
SELECT uo.*, u.full_name, u.email, u.avatar
FROM user_organisation uo
INNER JOIN users u ON u.id = uo.user_id
WHERE (u.full_name LIKE '%test%' OR u.email LIKE '%test%') AND
uo.organisation_id = 111;
Note also that in my answer I am using table aliases (which are abbreviations for full table names). Also, I removed the unnecessary backticks.

How would I use a join to create a view from two tables that displays the COUNT from another?

CREATE VIEW EMPJOBS AS
SELECT EMPLOYEE.employee_id AS ENUM,
CONCAT(EMPLOYEE.first_name,
' ' , EMPLOYEE.last_name) AS NAME,
EMPLOYEE.email AS EMAIL,
COUNT(JOBHISTORY.end_date) AS FINISHEDJOBS
FROM EMPLOYEE, JOBHISTORY
WHERE JOBHISTORY.employee_id = EMPLOYEE.employee_id;
This is what I have as a statement. I know it's wrong because it displays this:
It counts end_date all together as one big count. How could I make it count for every separate ENUM?
Thanks!
Add group by clause at the end.
CREATE VIEW EMPJOBS AS
SELECT
e.employee_id AS ENUM,
CONCAT(e.first_name,
' ' , e.last_name) AS NAME,
e.email AS EMAIL,
COUNT(j.end_date) AS FINISHEDJOBS
FROM EMPLOYEE e
inner join JOBHISTORY j
on j.employee_id = e.employee_id
group by
e.employee_id
,CONCAT(e.first_name,' ', e.last_name)
,e.email;
As a best practice, try to use proper join syntax to join table instead of using comma. Also put alias for tables names to avoid confusion and readability.
You can also do it in this way:
CREATE VIEW EMPJOBS AS
SELECT EMPLOYEE.employee_id AS ENUM,
CONCAT(EMPLOYEE.first_name, ' ' , EMPLOYEE.last_name) AS NAME,
EMPLOYEE.email AS EMAIL,
Select(COUNT(end_date) From JOBHISTORY jh where jh.employee_id = E.employee_id) AS FINISHEDJOBS
FROM EMPLOYEE E;

Mysql Concat two columns while searching with LIKE

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);

MySQL excutes select after where clause

This query gives an error unknown column company in where clause. I found that where clause runs first and select runs next. That could be the error here. But i dont know how to correct this in order to get company in result set.
SELECT trnsdriverid,
name,
(SELECT transcompany.name
FROM transcompany,
transcompdriver
WHERE transcompany.trnscompid = transcompdriver.trnscompid) AS 'company',
address,
dob,
license,
telephone
FROM transcompdriver
WHERE ? LIKE 'All'
OR name LIKE '%"+keyword+"%'
OR company LIKE '%"+keyword+"%'
OR trnsdriverid LIKE '%"+keyword+"%'
You can't reference column aliases in where statements. You should rewrite this query to use a JOIN and then do your filtering on the actual TransCompany.name column, for example:
select
d.trnsDriverID
,d.name
,c.name as [Company]
,d.address
,d.dob
,d.license
,d.telephone
from
TransCompDriver d
join
TransCompany c
on
c.trnscompid = d.trnscompid
where
? = 'All'
or
d.name like '%" + keyword + "%'
or
c.name like '%" + keyword + "%'
or
d.trnsDriverID like '%" + keyword + "%'
Also, don't use LIKE where a simple equality operator would do. I changed the query above to use = 'All'.
You should really be doing this using a join rather than a subselect. I would recommend this:
SELECT
d.trnsDriverID,
d.name,
c.name AS `company`,
d.address,
d.dob,
d.license,
d.telephone
FROM
TransCompDriver AS d
INNER JOIN TransCompany AS c
ON d.trnsCompID = c.trnsCompID
WHERE
? like 'All'
OR d.name LIKE '%"+keyword+"%'
OR `company` LIKE '%"+keyword+"%'
OR d.trnsDriverID LIKE '%"+keyword+"%'
The sub-query that pulls the column "company" does not have matching number of rows, try the join statement instead
select trnsDriverID, name, t1.name AS company, address, dob, license, telephone
from TransCompDriver JOIN (select trnsDriverID, name,
(select TransCompany.name from TransCompany LEFT JOIN TransCompDriver
ON TransCompany.trnsCompID=TransCompDriver.trnsCompID) AS t1
where ? like 'All' or name like '%"+keyword+"%' or company like '%"+keyword+"%'
or trnsDriverID like '%"+keyword+"%'

SQL SERVER QUERY to MYSQL QUERY

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.