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

后端 未结 9 1789
无人共我
无人共我 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:09

    What you need is an un-pivot operation. Microsoft SQL Server supports PIVOT and UNPIVOT as extensions to standard SQL. I don't know of any other brand of RDBMS that supports built-in pivot/unpivot functionality. Certainly MySQL does not support this.

    You can't use FULLTEXT search in this case, because as the docs say:

    Full-text indexes can be used only with MyISAM tables, and can be created only for CHAR, VARCHAR, or TEXT columns.

    And it wouldn't relieve you from needing to specify the columns anyway, because the MATCH() predicate needs you to list all columns in the fulltext index.

    If you can't restructure the table to store a row-per-month or a single column to encode all 12 months, then you do need to generate dynamic SQL.

提交回复
热议问题