0
Select maintable.name FROM maintable

JOIN genres genre1 USING (tmdb_id)
  JOIN genres genre2 USING (tmdb_id)
WHERE genre1.genres_name = 'Action'
  AND genre2.genres_name = 'Drama'

group by maintable.name

Here genres is table name. genres_name is column name. genres1 and genres2 are just nor a table name, nor a column name, they are just random name in the code.

This is my code, now How do i display all genres_name?

The genres is like:

tmdb_id    genres_name
1             Action
1             Crime
1             Drama
2             Horror 
2             Comedy
2             Drama

The main table isl ike

tmdb_id      movie_title
1            The Dark Knight
2            Logan
3            Wonder Woman

Let me know, if you need more information. (Please do not ask to show, what i tried. Trust me, it will make the question more confusing)

I want to echo the genres like:

The Dark Knight - Drama, Action, Crime
Josh Poor
  • 505
  • 1
  • 5
  • 12

2 Answers2

3

Of course, you need to use group_concat:

Select maintable.movie_title, group_concat(genres.genres_name) AS genres_name
FROM maintable
JOIN genres USING (tmdb_id)
GROUP BY maintable.tmdb_id
HAVING find_in_set('Action', genres_name) AND find_in_set('Drama', genres_name)

See demo here.

Note: How does find_in_set works, please see official doc.

Blank
  • 12,308
  • 1
  • 14
  • 32
  • I answered it 14 minutes ago, you 8 minutes, also I mentioned group concat 28 minutes ago in the comments, so yea basically. – ArtisticPhoenix Jun 30 '17 at 03:59
  • I cant be bothered with little things like "logic" and all that .. lol .. I was just saying. It's no big deal really. I did mine in my head, no testing. Besides I never used `find_in_set` before, I get that IN is like `Action` OR `Drama` ... and not AND, but whatever ... right logic and all that, cant spoon feed them. – ArtisticPhoenix Jun 30 '17 at 04:04
  • Thanks for the link, that was a good call on `find_in_set` I was just giving you a hard time.... lol ... need my entertainment tonight. I will have to remember that function, for future use. – ArtisticPhoenix Jun 30 '17 at 04:13
  • This one works, but it caused a big error. Pagination system is messed. If i use `AND` (both) Action & Drama. It show only 8 records per page (my limit is 10). And reset the offset to 0 whenever, i change the page. – John Doo Jun 30 '17 at 04:27
  • If i use `OR` (Action or Drama or both), then it shows 91 records per page instead of 10 And reset the offset to 0 whenever, i change the page. @forward sir – John Doo Jun 30 '17 at 04:28
  • use LIMIT and OFFSET – ArtisticPhoenix Jun 30 '17 at 05:00
  • Btw, this answer works after i put `DISTINCT` in group_concat. Other wise, same genres show 2 times – Josh Poor Jun 30 '17 at 05:07
  • @JoshPoor Glad can help. – Blank Jun 30 '17 at 05:16
1

I would try something like this. But this is the best I can do guessing at it in my head... ( sorry for any mistakes )

$Sql = "SELECT
    m.name,
    GROUP_CONCAT( g.genres_name ) as genres_list
FROM
    maintable AS m
JOIN
    genres AS g USING (tmdb_id)
WHERE
   g.genres_name IN('Drama', 'Action')
GROUP BY m.tmdb_id";

MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.

http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

Also note GROUP_CONCAT has a setting for the length, I don't recall what that is or how to change it, but it bit me in the butt one time. Basically it will truncate the list after a certain size, so be cautious of that.

See here: MySQL and GROUP_CONCAT() maximum length

AS I said I haven't tested this, but it seems you have a many to one relationship. Records in the maintable can have many related records in the genres table. Therefor, you should be able to group them on that relationship. Normally this would return 1 record for each pair ( same record in main table different in genre ) Without the group. The Group Concat allows you to compress that into a comma separated list.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • m is related to something or just random name? – Josh Poor Jun 30 '17 at 03:59
  • `maintable AS m` its an alias of maintable, because its shorter then typing maintable every time. Its just the first letter, that's how I do it typically, I cant be bothered with typing that all out all the time, I am a busy man. – ArtisticPhoenix Jun 30 '17 at 04:01
  • in other words you could do `maintable AS x2` if you wanted but that makes less sense. – ArtisticPhoenix Jun 30 '17 at 04:02
  • `Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN('Drama', 'Action') ` – Josh Poor Jun 30 '17 at 04:05
  • Obviously this wont work `'SELECT ... IN('Action' ... )'` try `"SELECT ... IN('Action' ... )"` check your quotes. That's not my fault. ( I'm assuming that's the cause ) as I don't put any quotes to make it a string..... – ArtisticPhoenix Jun 30 '17 at 04:08
  • as @Forward mentioned ( implied really ) using IN( .. ) is like using OR so 'Action' OR 'Drama' when you may want 'Action' AND 'Drama', although that requirement is not specifically clear in the original post. – ArtisticPhoenix Jun 30 '17 at 04:10
  • I am a bit slow, i am trying both answers – Josh Poor Jun 30 '17 at 04:13
  • Basically using `IN()` will return a result for a record that contains either 'Action' or 'Drama' ( or both ) and `find_in_set()` should work more like an AND that it must have both 'Action' AND 'Drama' if I understand it correctly. – ArtisticPhoenix Jun 30 '17 at 04:15
  • ` Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN('Drama', 'Action') ` – Josh Poor Jun 30 '17 at 05:03
  • Sorry but his answer was better, i wish i could choose both – Josh Poor Jun 30 '17 at 05:03
  • Try it without IS as in `IS IN(..)` just use `IN(..)` not sure what I was thinking. – ArtisticPhoenix Jun 30 '17 at 05:06
  • I wouldn't say his is better, the logic is different which may or may not be what you want. If that is the case ( you want AND ) pick the other one, no big deal to me. But don't let a little syntax error make that call, because they are different in that one is OR and one is AND. – ArtisticPhoenix Jun 30 '17 at 05:07
  • As I said I didn't test this, I don't even have a server running let alone MySQL so it was all in my head.... – ArtisticPhoenix Jun 30 '17 at 05:09
  • Yes, your answer is 100% correct. But i want `And` + `OR` Logic both. And it is possible in his answer, i guess (I just need to change And to OR using a variable) – Josh Poor Jun 30 '17 at 05:41