Group by sql query on comma joined column

后端 未结 6 1325
孤独总比滥情好
孤独总比滥情好 2020-12-10 22:57

My table structure is like below, "Mail" column can contain multiple email joined by comma

Data(int)

Mail(v

6条回答
  •  无人及你
    2020-12-10 23:13

    The correct thing to do would be to add a related table to store multiple emails. It is virtually always a bad design decision to store things in a comma delimited list as you have found in trying to query it. This generally means you need to create a related table as you have a one-to-many relationship. The task you want to do is trivial if you have properly related tables.

    I don't buy I can't change the table structure as an excuse. Unless this is a commercial product that your company doesn't own, you can change the structure, you just need to show management why it is necessary. Someone at your organization can change the database structure, find out who and convince him as to why it needs to change. If it is a commercial database, consider creating a trigger on the tble to populate a realted table that you create every time the email field is inserted updated or deleted. Then at least you only have to go through the splitting process once for each record change rather than every time the query is run.

提交回复
热议问题