I SELECT data from a MySQL database and I'm trying to replace WHERE column_name = 'name' with a wildcard like WHERE column_name LIKE '%name%'.
Sounds too easy, right?
The original code (which works perfectly) looks like this:
SELECT DISTINCT a.title_id, a.artist_id, fe_title.title, fe_artist.artist
FROM fe_title_artist AS a
INNER JOIN fe_title ON a.title_id = fe_title.id
INNER JOIN fe_artist ON a.artist_id = fe_artist.id
INNER JOIN fe_title_artist AS b
WHERE a.title_id=b.title_id
and b.artist_id=(SELECT id FROM fe_artist WHERE artist = 'Patrick Duffy')
What I tried and failed with looks like this:
SELECT DISTINCT a.title_id, a.artist_id, fe_title.title, fe_artist.artist
FROM fe_title_artist AS a
INNER JOIN fe_title ON a.title_id = fe_title.id
INNER JOIN fe_artist ON a.artist_id = fe_artist.id
INNER JOIN fe_title_artist AS b
WHERE a.title_id=b.title_id
and b.artist_id=(SELECT id FROM fe_artist WHERE artist LIKE '%Patrick%')
No results on this one.
Is a wildcard within a where clause just too much? Is the self join causing the problem? Is there a way to code this differently? Am I just stupid?
I hope someone can help me. Thank you.
Its due to Subquery returns more than 1 rows. Please use below Query.
SELECT DISTINCT a.title_id, a.artist_id, fe_title.title, fe_artist.artist
FROM fe_title_artist AS a
INNER JOIN fe_title ON a.title_id = fe_title.id
INNER JOIN fe_artist ON a.artist_id = fe_artist.id
INNER JOIN fe_title_artist AS b
WHERE a.title_id=b.title_id
and b.artist_id IN (SELECT id FROM fe_artist WHERE artist LIKE '%Patrick%')
Related
We are maintaining a history of Content. We want to get the updated entry of each content, with create Time and update Time should be of the first entry of the Content. The query contains multiple selects and where clauses with so many left joins. The dataset is very huge, thereby query is taking more than 60 seconds to execute. Kindly help in improving the same. Query:
select * from (select * from (
SELECT c.*, initCMS.initcreatetime, initCMS.initupdatetime, user.name as partnerName, r.name as rightsName, r1.name as copyRightsName, a.name as agelimitName, ct.type as contenttypename, cat.name as categoryname, lang.name as languagename FROM ContentCMS c
left join ContentCategoryType ct on ct.id = c.contentType
left join User user on c.contentPartnerId = user.id
left join Category cat on cat.id = c.categoryId
left join Language lang on lang.id = c.languageCode
left join CopyRights r on c.rights = r.id
left join CopyRights r1 on c.copyrights = r1.id
left join Age a on c.ageLimit = a.id
left outer join (
SELECT contentId, createTime as initcreatetime, updateTime as initupdatetime from ContentCMS cms where cms.deleted='0'
) as initCMS on initCMS.contentId = c.contentId WHERE c.deleted='0' order by c.id DESC
) as temp group by contentId) as c where c.editedBy='0'
Any help would be highly appreciated. Thank you.
Just a partial eval and suggestion because your query seems non properly formed
This left join seems unuseful
FROM ContentCMS c
......
left join (
SELECT contentId
, createTime as initcreatetime
, updateTime as initupdatetime
from ContentCMS cms
where cms.deleted='0'
) as initCMS on initCMS.contentId = c.contentId
same table
the order by (without limit) in a subquery in join is unuseful because join ordered values or unordered value produce the same result
the group by contentId is strange beacuse there aren't aggregation function and the sue of group by without aggregation function is deprecated is sql
and in the most recente version for mysql is not allowed (by deafult) if you need distinct value or just a rows for each contentId you should use distinct or retrive the value in a not casual manner (the use of group by without aggregation function retrive casual value for not aggregated column .
for a partial eval your query should be refactored as
SELECT c.*
, c.initcreatetime
, c.initupdatetime
, user.name as partnerName
, r.name as rightsName
, r1.name as copyRightsName
, a.name as agelimitName
, ct.type as contenttypename
, cat.name as categoryname
, lang.name as languagename
FROM ContentCMS c
left join ContentCategoryType ct on ct.id = c.contentType
left join User user on c.contentPartnerId = user.id
left join Category cat on cat.id = c.categoryId
left join Language lang on lang.id = c.languageCode
left join CopyRights r on c.rights = r.id
left join CopyRights r1 on c.copyrights = r1.id
WHERE c.deleted='0'
) as temp
for the rest you should expiclitally select the column you effectively need add proper aggregation function for the others
Also the nested subquery just for improperly reduce the rows don't help performance ... you should also re-eval you data modelling and design.
I have 3 tables concerning complains. The first table consists of the complain information itself, 2nd one is the complain_review with status_id, and the 3rd is the status_id table consisting status information. I'm trying to select the complain_desc from complain and latest status_id from complain_review (sort by date desc) and couple that with complain_status information.
This is what I've tried (no success so far):
SELECT c1.complain_desc, c2.status_id, c2.name as statusDesc from complain c1
left join
(SELECT c3.status_id, c4.name, c3.complain_id FROM complain_review c3
inner join complain_status c4 on c4.id=c3.status_id ORDER by c3.date DESC) c2
on c2.complain_id=c1.id
this is the updated example provided by #maheshiv
.. I've searched through the site but I don't exactly know what keyword to search concerning this matter :(
Edit: I've build a schema at http://sqlfiddle.com/#!9/d86a7a/2 so perhaps somebody could give take a better look at the tables
Edit: Perhaps this would be the closest as I could get .. and working!
SELECT c.complain_desc, cr1.status_id, cs.name
FROM complain c
INNER JOIN complain_review cr1 ON c.id=cr1.complain_id
INNER JOIN complain_status cs ON cs.id=cr1.status_id
WHERE cr1.date = (SELECT MAX(cr2.date) FROM complain_review cr2
WHERE cr1.complain_id=cr2.complain_id)
I'm trying to select the complain_desc from complain and latest status_id from complain_review (sort by date desc) and couple that with complain_status information.
This is a very common question on Stack Overflow. You can follow the greatest-n-per-group to find many solutions.
Here's a solution using your example:
SELECT c.complain_desc, latest_cr.status_id, cs.name AS status_desc
FROM complain AS c
INNER JOIN (
SELECT complain_id, status_id
FROM (
SELECT cr.complain_id, cr.status_id,
IF(#cgroup=cr.complain_id, #rownum:=#rownum+1, 1) AS rownum,
(#cgroup:=cr.complain_id)
FROM (SELECT #cgroup:=0, #rownum:=1) AS _init
CROSS JOIN complain_review AS cr
ORDER BY cr.complain_id DESC, cr.date DESC
) AS n
WHERE n.rownum = 1
) AS latest_cr
ON c.id=latest_cr.complain_id
INNER JOIN complain_status AS cs
ON cs.id = latest_cr.status_id;
Here's a different solution using no subqueries:
SELECT c.complain_desc, cr1.status_id, cs.name AS status_desc
FROM complain AS c
INNER JOIN complain_review AS cr1
ON cr1.complain_id = c.id
LEFT OUTER JOIN complain_review AS cr2
ON cr2.complain_id = c.id AND (cr2.date > cr1.date OR cr2.date = cr1.date AND cr2.id > cr1.id)
INNER JOIN complain_status AS cs
ON cs.id = cr1.status_id
WHERE cr2.id IS NULL;
I think you may need this query,
I believe max status_id is the latest status for complaint. As per http://sqlfiddle.com/#!9/d86a7a/15
select c1.complain_desc, c2.status_id, c3.name from complain c1 inner join (select complain_id, max(status_id) from complain_review group by complain_id) c2 on c1.id=c2.complain_id inner join complain_status c3 on c3.id=c2.status_id;
i have this little mysql query :
select t.title FROM title t
inner join movie_keyword mk on mk.movie_id = t.id
inner join keyword k on k.id = mk.keyword_id
where k.keyword IN (
select k.keyword
FROM title t
inner join movie_keyword mk on mk.movie_id = t.id
inner join keyword k on k.id = mk.keyword_id
where t.id = 166282
)
LIMIT 15
as you can see it will return all titles from title that have at least one the same keyword that have movie with id 166282.
Now i have problem, because i want also count how many keywords was matched in IN operator(let's say i want to see only titles that have 3 or more the same keywords), i tried something with aggregate functions, but everything failed, so i came here with my problem. Maybe somebody can give me some advice, or code example.
I'm not also sure, if this "subquery way" is good, so if there are some better options how i should solve my problem, I am open to any suggestions or tips.
Thank you!
#Edit
So after some problems, i have one more. This is my current query :
SELECT s.title,s.vote,s.rating,count(dk.key) as keywordCnt, count(dg.name) as genreCnt
FROM series s
INNER JOIN series_has_genre shg ON shg.series_id = s.id
INNER JOIN dict_genre dg ON dg.id = shg.dict_genre_id
INNER JOIN series_has_keyword shk ON shk.series_id = s.id
INNER JOIN dict_keyword dk ON dk.id = shk.dict_keyword_id
WHERE dk.key IN (
SELECT dki.key FROM series si
INNER JOIN series_has_keyword shki ON shki.series_id = si.id
INNER JOIN dict_keyword dki ON dki.id = shki.dict_keyword_id
WHERE si.title LIKE 'The Wire'
)
and dg.name IN (
SELECT dgo.name FROM series so
INNER JOIN series_has_genre shgo ON shgo.series_id = so.id
INNER JOIN dict_genre dgo ON dgo.id = shgo.dict_genre_id
WHERE so.title LIKE 'The Wire'
)
and s.production_year > 2000
GROUP BY s.title
ORDER BY s.vote DESC, keywordCnt DESC ,s.rating DESC, genreCnt DESC
LIMIT 5
Problem is, it is very, very, very slow. Any tips what i should change, to run it faster ?
Will this work for you:
select t.title, count(k.keyword) as keywordCount FROM title t
inner join movie_keyword mk on mk.movie_id = t.id
inner join keyword k on k.id = mk.keyword_id
where k.keyword IN (
select ki.keyword
FROM title ti
inner join movie_keyword mki on mki.movie_id = ti.id
inner join keyword ki on ki.id = mki.keyword_id
where ti.id = 166282
) group by t.title
LIMIT 15
Note that I have changed the table names inside the nested query to avoid confusion.
I have query like this.
SELECT account.AccountNumber, account.Name, Sum(agro.price*agro.qty) AS Expr1
FROM account,data INNER JOIN (agro INNER JOIN data ON agro.BillNo = data.BillNo) ON
account.AccountNumber = data.acno
GROUP BY account.AccountNumber, account.Name;
I want to add where db='true' this columns is of 'data' table then how can i do pls help me?
Try this:
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
You had some confusion in your JOINs, but i think this is what you were aiming for
I'm working with the following DB model
A client has asked me to make a few changes to their DB, I haven't played with relational databases in a few years, generally work with flat DB's
Could someone help me on my way with giving me an example of how the following query would work.
say if I wanted to
select all films with a title like '%Matrix%' under a certain genreID
Any assistance would be greatly appreciated
Use an inner join to join the three tables
SELECT F.title
FROM film F
INNER JOIN filmgenres FG
ON F.filmid = FG.film_filmid
INNER JOIN genres G
ON FG.genres_genreid = G.genreid
WHERE F.title LIKE '%Matrix%' AND G.genre = "Some Genre"
You need to join the tables first,
SELECT a.*, c.Genre
FROM Film a
INNER JOIN FilmGenres b
ON a.FilmID = b.Film_FilmID
INNER JOIN Genres c
ON b.Genre_GenreID
WHERE a.Title LIKE '%matrix%'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
but if you want to search for a specific genre, then you might add a condition as well,
SELECT a.*, c.Genre
FROM Film a
INNER JOIN FilmGenres b
ON a.FilmID = b.Film_FilmID
INNER JOIN Genres c
ON b.Genre_GenreID
WHERE a.Title LIKE '%matrix%' AND c.GenreID = 10
SELECT Film.*
FROM Film f
JOIN FilmGenres fg ON g.Film_FilmID = f.FilmID
WHERE fg.Genres_GenreID = 3
AND Title LIKE '%Matrix%'
SELECT * FROM Film INNER JOIN FilmGenres ON Film.FilmID = FilmGenres.Film_FilmID
WHERE FilmGenres.Genres_GenreID = 1
AND
Film.Title like '%Matrix%'
It's a pretty simple join:
select
Film.*
from
Film
inner join FilmGenres on
Film.FilmID = FilmGenres.Film_FilmID
where
Film.Title like "%Matrix%" and
FilmGenres.Genres_GenreID = ?