Theres my two tables:
mysql> desc riddims;
+---------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+-------------------+-------+
| riddim | varchar(255) | NO | MUL | NULL | |
| genre | varchar(9) | NO | | NULL | |
| youtube | varchar(11) | NO | | NULL | |
| image | varchar(11) | NO | | NULL | |
| last_modified | timestamp | NO | | CURRENT_TIMESTAMP | |
+---------------+--------------+------+-----+-------------------+-------+
5 rows in set (0.00 sec)
mysql> desc tunes;
+---------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+-------------------+-------+
| riddim | varchar(255) | NO | MUL | NULL | |
| artist | varchar(255) | NO | MUL | NULL | |
| tune | varchar(255) | NO | | NULL | |
| label | varchar(255) | NO | | NULL | |
| producer | varchar(255) | NO | | NULL | |
| year | varchar(4) | NO | | NULL | |
| lyrics | text | NO | | NULL | |
| flag | varchar(12) | NO | | NULL | |
| last_modified | timestamp | YES | | CURRENT_TIMESTAMP | |
+---------------+--------------+------+-----+-------------------+-------+
What I got so far is
SELECT DISTINCT riddim FROM tunes WHERE year = '2012' ORDER BY last_modified DESC LIMIT 20
it prints the last 20 added/modified entries of the ‘riddim’ column. How do I add the proper information about each riddim from the ‘riddims’ table to each row grabbed from table ‘tunes’?
I assume I have to JOIN them somehow using the “riddim” column as this is the “key” that both tables have in common, I just couldn’t figure out yet how it works.
My goal is to print “riddim, year, image, label” of the last 20 riddims entries of this year (using php), where only the ‘tunes’ table holds information about the year and label.
1 Answer