简体   繁体   English

搜索逗号分隔的mysql字段的最佳方法

[英]Best way to search a comma separated mysql field

I have been given the task to combine 2 mysql fields into one and then make it so that the new field can be searched. 我已经获得了将2个mysql字段合并为一个,然后进行合并以便可以搜索新字段的任务。 The 2 fields I had to combine in my database where previous year registered and current years registered. 我必须在我的数据库中合并这两个字段,前一个注册年份和当前注册的年份。 The format both these fields are in are dd/mm/yyyy. 这两个字段的格式均为dd / mm / yyyy。 I have combined these fields into a field called Years Registered whih is still in the same format dd/mm/yyyy but has the years registered seperated by a comma(,). 我将这些字段组合成一个字段,称为“注册年数”,尽管其格式仍为dd / mm / yyyy,但注册的年数之间用逗号(,)分隔。 I am wondering how I would go about performing a couple different kinds of querys on this column. 我想知道如何在此列上执行几种不同类型的查询。 The mysql queries I have to perform are: Show All() , Show All between dates: mm/yyyy and mm/yyyy , Before: mm/yyyy , After: mm/yyyy 我必须执行的mysql查询是:Show All(),在日期之间显示所有:mm / yyyy和mm / yyyy,之前:mm / yyyy,之后:mm / yyyy

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks for your time. 谢谢你的时间。

Do not do this! 不要这样做! I do not know how it is exactly possible (some SQL Stringoperations and Datefunctions in a storedprocedurem i presume), but it will surely kill performance of your database. 我不知道这是怎么可能的(我假设存储过程中的某些SQL Stringoperations和Datefunctions),但这肯定会破坏数据库的性能。 use a relation for this. 为此使用关系。

This is: 这是:

  • way faster 更快
  • more expandable (eg. for three dates..) 更具扩展性(例如,三个日期)
  • easier to code 更容易编码
  • much better understandable 更容易理解
  • more portable to other databases 更易于移植到其他数据库

If you have problems with existing platforms you have to support, use a code base where both alternatives are supported. 如果必须支持现有平台,请使用同时支持这两种选择的代码库。 This is still easier and better to maintain than to use a comma-separated list 与使用逗号分隔的列表相比,此方法仍然更容易维护且更好

I don't like it but if you need you can use the next solution: 我不喜欢它,但是如果您需要,可以使用下一个解决方案:

extract date using start_date = STR_TO_DATE(SUBSTRING(your_new_field, 1, 10)) and end_date=STR_TO_DATE(SUBSTRING(your_new_field, 12, 10)) 使用start_date = STR_TO_DATE(SUBSTRING(your_new_field,1,10))和end_date = STR_TO_DATE(SUBSTRING(your_new_field,12,10))提取日期

Your database would be breaking 'First Normalized Form (1NF)' and would be highly ineffecient. 你的数据库将打破“第一标准化表格(1NF)”和将是非常 ineffecient。

In order to search for a selected date, you would either have to query all rows in the table, or use LIKE which is also very sluggish. 为了搜索选定的日期,您要么必须查询表中的所有行,要么使用同样很慢的LIKE

Whoever is asking you to do this should read this article on database normalization. 要求您执行此操作的人应阅读有关数据库规范化的文章

What is wrong with using two DATE , or DATETIME fields and the formatting them outside of MySQL? 使用两个DATEDATETIME字段并在MySQL外部格式化它们又怎么了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM