Matching a value to multiple columns (in one statement) from a table using MySQL

后端 未结 9 1810
无人共我
无人共我 2021-02-08 21:29

I\'m working with a table in MySQL that contains the following columns:

id, january, february, march, april, etc

The data in the table looks li

9条回答
  •  失恋的感觉
    2021-02-08 22:04

    In my opinion this is caused from what appears to be poor table design (I am guessing that is just an example to simply your problem, but bear with me, and I think this concept could be modeled better.
    Here are the tables:

      Things        Things_Months      Months
       thing_id       thing_id           Month_id
       thing_info     month_id           Month_Name
                      thing_month_id     order_field
    

    and here would be the sql:

     select thing_info, month_name
      from things, things_months, months
      where things.thing_id = things_months.thing_id
        and things_months.month_id = months.month_id 
      order by things.things_info, months.order_field
    

    The results would be

      thingy 1, January
      thingy 1, February
      thingy 2, April
      thingy 3, November
      thingy 3, December
    

提交回复
热议问题