Attaching categories from one table to entries in another MySQL

前端 未结 3 573
攒了一身酷
攒了一身酷 2021-01-23 15:31

I have a database which takes user submitted data, the entries from which I want to group under one or several of about 10 categories.

So for example, you add your entr

3条回答
  •  迷失自我
    2021-01-23 15:54

    To get a business data with all categories you can do,

    SELECT bus.*, cat.category_name FROM business bus 
        JOIN tbl_works_categories twc USING (bus_id) 
        JOIN categories cat USING (category_id) 
        WHERE bus.bus_id = ?
    

    To retrieve business data from category name, just switch the WHERE param

    SELECT bus.*, cat.category_name FROM business bus 
        JOIN tbl_works_categories twc USING (bus_id) 
        JOIN categories cat USING (category_id) 
        WHERE cat.category_name = ?
    

提交回复
热议问题