I’ve worked with mysql for some time but have never had to do anything anywhere near complicated. I’m working on a new project and need a little push in the right direction. Lets say I want to have a table that stores cats. Lets say I want to store the cats name, and food that the cat likes. Then lets say I want to query all cats that like fish, milk, and mice. I don’t want to have the ‘cats’ table have rows for ‘fish’ ‘milk’ ‘mice’ that can be yes or no or 1 or 0. I think want I’m looking for is having a separate ‘foods’ table and then use the join statement. But I have no clue where to start looking or what to search for. Another way to think about this would be if you had a blog, and wanted posts to have categories. How would I store which posts are in which categories, and how would I query for posts by category?
I realize this is probably a very basic question, and would be happy with even a link to a tutorial explaining the structure / commands needed to pull this off! Thanks a lot!
Query: Select cats that like fish
SELECT
name
FROM
cats c
INNER JOIN
cat_food cf
ON
cf.fid = 2 -- Assuming food ID#2 is 'fish'
Query: Select cats that like fish, milk, or mice
SELECT
c.name
FROM
cats c
INNER JOIN
cat_food cf
ON
cf.fid IN (1, 2, 3)
GROUP BY
c.name
SCHEMA
CREATE TABLE IF NOT EXISTS
cats(idint(10) unsigned NOT NULL AUTO_INCREMENT,namevarchar(255) COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (
id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
INSERT INTO
cats(id,name) VALUES(1, 'Sassy'),
(2, 'Tiger');
CREATE TABLE IF NOT EXISTS
cat_food(cidint(10) unsigned NOT NULL COMMENT 'Cat ID',fidint(10) unsigned NOT NULL COMMENT 'Food ID',UNIQUE KEY
cid_fid(cid,fid),KEY
fid(fid)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO
cat_food(cid,fid) VALUES(1, 1),
(2, 2),
(1, 3);
CREATE TABLE IF NOT EXISTS
foods(idint(10) unsigned NOT NULL AUTO_INCREMENT,namevarchar(255) COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (
id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
INSERT INTO
foods(id,name) VALUES(1, 'Fish'),
(2, 'Milk'),
(3, 'Mice');
ALTER TABLE
cat_foodADD CONSTRAINT
cat_food_ibfk_2FOREIGN KEY (fid) REFERENCESfoods(id) ON DELETE CASCADE,ADD CONSTRAINT
cat_food_ibfk_1FOREIGN KEY (cid) REFERENCEScats(id) ON DELETE CASCADE;