In MySQL, How can get the intersection of the 9 select results? - mysql

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
. . .

Related

mysql join if string contains similar values

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

MYSQL: AND statement causing expected results to not display [duplicate]

This question already has answers here:
MySQL query finding matches across multiple rows
(2 answers)
MySQL query to search multiple attributes and value_id
(1 answer)
Closed 3 years ago.
When I run the below query, I expect to get one result.
However, I get no results.
SELECT listings.*
FROM `listings`
LEFT JOIN users
ON listings.user_id = users.user_id
LEFT JOIN listing_attributes
ON listings.listing_id = listing_attributes.listing_id
WHERE users.account_status = 1
AND ( listing_attributes.name LIKE 'collection_city' AND listing_attributes.content LIKE '%d%' )
AND ( listing_attributes.name LIKE 'collection_post_code' AND listing_attributes.content LIKE '%e%' )
GROUP BY listings.listing_id
I have identified the problem as these two lines
AND ( listing_attributes.name LIKE 'collection_city' AND listing_attributes.content LIKE '%d%' )
AND ( listing_attributes.name LIKE 'collection_post_code' AND listing_attributes.content LIKE '%e%' )
If I remove either of the lines, I get the result I expect.
I am thinking something is wrong with the way I am encompassing the results in the ().
In essence I am wishing to check that multiple values exist in listing_attributes for a result to be as needed.
I think that your logic is not correct. You must use the operator OR for this condition. Otherwise the condition will be false.
{
( listing_attributes.name LIKE 'collection_city' AND listing_attributes.content LIKE '%d%' )
OR ( listing_attributes.name LIKE 'collection_post_code' AND listing_attributes.content LIKE '%e%' )
}
Please use below query, Try moving the condition to on clause
SELECT listings.*
FROM `listings`
LEFT JOIN users
ON listings.user_id = users.user_id AND users.account_status = 1
LEFT JOIN listing_attributes
ON listings.listing_id = listing_attributes.listing_id AND ( listing_attributes.name LIKE 'collection_city' AND listing_attributes.content LIKE '%d%' ) AND ( listing_attributes.name LIKE 'collection_post_code' AND listing_attributes.content LIKE '%e%' )
GROUP BY listings.listing_id;
Given that listings.listing_id is a key of listings you can use a HAVING clause checking for the sums of the logical expressions are greater then 0. This works because logical expressions in numeric context evaluate to 0 or 1 in MySQL.
Your join with user silently becomes an inner join because of the WHERE as NULLs cannot equal 1. Since the NULLs from the left join of listing_attributes cannot make the HAVING clause true you can also convert that join to an inner join.
SELECT listings.*
FROM listings
INNER JOIN users
ON listings.user_id = users.user_id
INNER JOIN listing_attributes
ON listings.listing_id = listing_attributes.listing_id
WHERE users.account_status = 1
GROUP BY listings.listing_id
HAVING sum(listing_attributes.name LIKE 'collection_city'
AND listing_attributes.content LIKE '%d%') > 0
AND sum(listing_attributes.name LIKE 'collection_post_code'
AND listing_attributes.content LIKE '%e%') > 0;

MySQL Query show results based on multiple filters/tags

This has been asked in different ways before, but I can't seem to get something that works for what I need exactly.
The goal here is to make a search query that returns Photos based on tags that are selected. Many tags can be applied to the filter simultaneously, which would need to make it so that the query only returns photos that have ALL of the tags selected. Think of any major web shop where you are narrowing down results after performing a basic keyword search.
Table1: Photos
ID|Title|Description|URL|Created
Table2: PhotosTagsXref
ID|PhotoId|TagId
Table3: PhotosTags
ID|Title|Category
What I have:
SELECT p.* FROM `PhotosTagsXref` AS pt
LEFT JOIN `Photos` AS p ON p.`ID` = pt.`PhotoId`
LEFT JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
WHERE p.`Description` LIKE "%test%" AND
????
GROUP BY p.`ID`
ORDER BY p.`Created` DESC LIMIT 20
The ???? is where I've tried a bunch of things, but stumped. Problem is I can easily find a result set that contains photos with one tag or another, but if applying 2, 3, or 4 tags we'd need to only return photos that have entries for all of those tags in the database. I think this will involve combining result sets but not 100% sure.
Example:
Photo 1 Tags: Blue, White, Red
Photo 2 Tags: Blue
Searching for a photo with tags of 'blue' returns both photos, searching for a photo with tags of 'blue' and 'white' returns only Photo 1.
Supposing the requested set of tags is (red,blue) you can do:
SELECT * FROM `Photos`
WHERE `Description` LIKE "%test%"
AND `ID` IN (
SELECT pt.`PhotoId` FROM `PhotosTagsXref` AS pt
JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
WHERE t.Title in ('red','blue') /* your set here */
GROUP BY pt.`PhotoId` HAVING COUNT(DISTINCT t.`TagId`)=2 /* # of tags */
)
ORDER BY `Created` DESC LIMIT 20
Apparently, the tag set needs to be created dynamically, as well as its count.
Note: I'm counting DISTINCT TagIDs because I don't know your table's constraints. If PhotosTagsXRef had a PK/UNIQUE (PhotoId,TagId) and PhotosTags had a PK/UNIQUE (TagId), then COUNT(*) would suffice.
Admittedly a bit ugly. But assuming that PhotosTags.Category has the 'Blue', 'White', etc, try something along this line.
SELECT p.*
From `Photos` AS p
WHERE p.`Description` LIKE "%test%" AND
AND Exists
( Select 1 FROM `PhotosTagsXref` AS pt
Inner JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
Where pt.`PhotoId` = p.`ID`
And t.Category = 'FirstCatToSearch'
)
AND Exists
( Select 1 FROM `PhotosTagsXref` AS pt
Inner JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
Where pt.`PhotoId` = p.`ID`
And t.Category = 'SecondCatToSearch'
)
AND Exists
( ...
)
...
SELECT p.* FROM `PhotosTagsXref` AS pt
LEFT JOIN `Photos` AS p ON p.`ID` = pt.`PhotoId`
LEFT JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
inner join (select PhotoId from PhotosTagsXref
LEFT JOIN `PhotosTags` AS t
ON pt.`TagId` = t.`ID`
where (t.title = 'cond 1' or t.title = 'cond 2' ...)
--where t.title in (list condition) **this works as well**
having count(1) = (count of conditions) ) filter
on filter.photoID = pt.PhotoID
WHERE p.`Description` LIKE "%test%"
GROUP BY p.`ID`
ORDER BY p.`Created` DESC LIMIT 20
That should work, I made some assumptions on what column to use for the filter and joins, you may need to retool...the inner join functions as a filter and should pull out only records that have the number of matches equal to the total of the number of matches submitted. Now you just need a language to plug in those conditions and condition count values.

mysql like and different value not working

I'm doing a form of research every things work fine except the clause <> i don't know why happen. any help?
this is my query
SELECT users . * , image_upload.name_image
FROM users
LEFT JOIN image_upload ON users.profile_image = image_upload.id_image
WHERE users.id <>1
AND LOWER( users.name ) LIKE 'f%'
OR LOWER( users.surname ) LIKE 'f%'
LIMIT 5
when start the query, it shows the row with the id = 1 logically is not correct.
Try this:
SELECT users . * , image_upload.name_image
FROM users
LEFT JOIN image_upload ON users.profile_image = image_upload.id_image
WHERE users.id <>1
AND (LOWER( users.name ) LIKE 'f%'
OR LOWER( users.surname ) LIKE 'f%')
LIMIT 5
Remember that the OR statement really has to be in parenthesis due to the way that operators are processed.
Here's a link for you:
http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

Join two strings from db together

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