I need to query two columns "oc_filter_group_description.name" and "oc_filter.name" and get all matching results.
The alias seems to be causing me the problem. I've tried using a sub query but being a complete novice I really don't know what I'm doing.
Here is the code as it stands. The OR clause is what needs adapting
SELECT *,(SELECT name FROM oc_filter_group_description fgd
WHERE f.filter_group_id = fgd.filter_group_id) AS `group`
FROM oc_filter f LEFT JOIN oc_filter_description fd ON (f.filter_id = fd.filter_id)
WHERE fd.name LIKE '% **QUERY GOES HERE** %'
OR 'group' LIKE '% **QUERY GOES HERE** %'
I have spent far too long trying to get this to work I thought I better call in re-enforcements. Thanks in advance
You can't reference column aliases defined in the select in the where clause. This is your query:
SELECT *,
(SELECT name
FROM oc_filter_group_description fgd
WHERE f.filter_group_id = fgd.filter_group_id
) AS `group`
FROM oc_filter f LEFT JOIN
oc_filter_description fd
ON f.filter_id = fd.filter_id
WHERE fd.name LIKE '% **QUERY GOES HERE** %' OR
'group' LIKE '% **QUERY GOES HERE** %';
It has two obvious problems. The first is that 'group' is a string constant not a column reference. Only use single quotes for string literals. It avoids this type of problem. The second is that it refers to the group defined in the select clause.
The best solution is to replace the subquery in the select with another join:
SELECT *, fgd.name as group_name
FROM oc_filter f LEFT JOIN
oc_filter_description fd
ON f.filter_id = fd.filter_id LEFT JOIN
oc_filter_group_description fgd
ON f.filter_group_id = fgd.filter_group_id
WHERE fd.name LIKE '% **QUERY GOES HERE** %' OR
fgd.name LIKE '% **QUERY GOES HERE** %';
Note that I also changed the name group to group_name, so it doesn't conflict with a MySQL reserved words. Voila, no quotes are needed.
This might look a minor thing, but you shouldn't use backticks and whenever possible avoid keywords:
SELECT *, (
SELECT name FROM oc_filter_group_description fgd
WHERE f.filter_group_id = fgd.filter_group_id) AS T_GROUP
FROM oc_filter f LEFT JOIN oc_filter_description fd ON (f.filter_id = fd.filter_id)
WHERE fd.name LIKE '% **QUERY GOES HERE** %'
OR T_GROUP LIKE '% **QUERY GOES HERE** %'
Hope this helped.
You made it much more complicated than necessary. All you need is something like this:
select just the fields you need
from oc_filter f LEFT JOIN oc_filter_description fd ON (f.filter_id = fd.filter_id)
and (fd.name like '%static text goes here, not a query%'
or `group` like '%static text goes here, not a query%')
Notice that the filters are now part of the join as opposed to a where clause. If they were in a where clause, the left join would effectively become an inner join.
Related
Assuming I have a table that has column Description with below values:
His name was Jacob King
One of the guy was Jacob. He was taller than them
How do I join these two rows (in MySql) since they both contain the word Jacob? There will be more rows with other words too so Jacob is not the only word than can appear in more than one row. What I want is a way of joining rows with similar words appearing in them.
I tried using left join with LIKE keyword as shown below but it didn't work since i am just looking for similar word in sentences
select * from (SELECT id,description FROM `statement`) f1
left JOIN (SELECT id,description FROM `statement`) f2
on f1.description like concat('%' ,f2.description,'%')
The above doesn't work, I think because I am looking for a word as opposed to the entire sentence
You can try usinga join
SELECT concat (s1.description , ' ', s2.description)'
FROM `statement` s1
INNER JOIN `statement` s2 ON s1.description like ('%Jacob%')
AND s2.description like ('%Jacob%')
or using an input
set #my_word= 'jacob';
SELECT concat (s1.description , ' ', s2.description)'
FROM `statement` s1
INNER JOIN `statement` s2 ON lower(s1.description) like concat('%', #my_word, '%')
AND lower(s2.description) like concat('%', #my_word, '%')
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'm trying to run this code but i'm getting errors.
Any ideas why?
SELECT notes.project_id, notes.title, notes.description
FROM notes, project_members
WHERE notes.title LIKE '%second%' OR notes.description LIKE '%second%'
WHERE notes.project_id = project_members.project_id AND project_members.user_id = '7'
Since you only can have one WHERE clause you could use a inner join like this:
SELECT notes.project_id, notes.title, notes.description
FROM project_members pm
INNER JOIN notes ON notes.project_id = pm.project_id AND pm.user_id = 7
WHERE notes.title LIKE '%second%' OR notes.description LIKE '%second%'
You have two WHERE clauses, but single SELECT only allows one. But, this would easily be fixed if you used proper, explicit JOIN syntax:
SELECT n.project_id, n.title, n.description
FROM notes n JOIN
project_members p
ON n.project_id = p.project_id
WHERE (n.title LIKE '%second%' OR n.description LIKE '%second%') AND
p.user_id = '7';
Note:
Always use proper, explicit JOIN syntax. Never use commas in the FROM clause.
Table aliases make the query easier to write and to read.
I'm pretty sure your WHERE conditions are missing parentheses, for your intended logic.
If user_id is a number of any sort, then don't use single quotes around "7". This can confuse both the compiler and people.
There should be single WHERE clause.
SELECT notes.project_id, notes.title, notes.description
FROM notes, project_members
WHERE
(notes.title LIKE '%second%' OR notes.description LIKE '%second%')
AND notes.project_id = project_members.project_id AND project_members.user_id = '7';
Query would look something above to AND between your LIKE conditions.
You may also consider using JOIN, Like this.
SELECT notes.project_id, notes.title, notes.description
FROM notes
JOIN project_members
ON notes.project_id=project_members.project_id
WHERE
(notes.title LIKE '%second%' OR notes.description LIKE '%second%')
AND project_members.user_id = '7';
I am making the sql code. I have met the barrier, that is, so many select sentences in SQL query. Finally I want to get to the intersection of 9 select results
My sql code is same as below, just 1 select sentence. 8 select sentences are different from only search word, eg) cholera, diarrhea, fever, vomit, nausea, etc
First select sentence. Don't be suprised. That code is simple and repeatedly.
(SELECT code_co.code, code_co.disease_co, code_en.disease_en
FROM code_co
LEFT JOIN code_en ON code_en.code = code_co.code
LEFT JOIN note ON note.code = code_co.code
LEFT JOIN inclusion ON inclusion.code = code_co.code
LEFT JOIN exclusion ON exclusion.code = code_co.code
LEFT JOIN ds ON code_co.code = ds.code
LEFT JOIN tx ON code_co.code = tx.code
LEFT JOIN sx ON code_co.code = sx.code
WHERE
note LIKE CONCAT( '%', (
SELECT ds_word.ds_en
FROM ds_word
WHERE ds_co LIKE '%cholera%'
LIMIT 0 , 1
), '%' )
or
ds_content LIKE CONCAT( '%', (
SELECT ds_word.ds_en
FROM ds_word
WHERE ds_co LIKE '%cholera%'
LIMIT 0 , 1
), '%' )
...
inclusion LIKE CONCAT( '%', (
SELECT ds_word.ds_en
FROM ds_word
WHERE ds_co LIKE '%cholera%'
LIMIT 0 , 1
), '%' )
)
Below is the captured picture on phpmyadmin.
Really working code!
And 2nd select sentence is same as first sentence except cholera. Cholera is my search word.
In this way, I have 9 select sentences.
I want to get the intersection, but in MySQL, How can I care?
Intersect or minus can be used in just 2 sentences. (Right?)
(1st select sentence)
intersect
(2nd select sentence)
intersect
(3rd select sentence)
...
This way is right?
Please help me.
Thank you for your advice
You do the "intersect" by using and in the where clause. Using or is equivalent to a "union".
Also, you can simplify your expression by doing:
LEFT JOIN sx ON code_co.code = sx.code
CROSS JOIN (SELECT concat('%', ds_word.ds_en, '%') as pattern
FROM ds_word
WHERE ds_co LIKE '%cholera%'
LIMIT 0 , 1
) const
WHERE note LIKE const.pattern and
ds_content like const.pattern and
. . .
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]}%'