<small id='loPyY'></small><noframes id='loPyY'>

      <i id='loPyY'><tr id='loPyY'><dt id='loPyY'><q id='loPyY'><span id='loPyY'><b id='loPyY'><form id='loPyY'><ins id='loPyY'></ins><ul id='loPyY'></ul><sub id='loPyY'></sub></form><legend id='loPyY'></legend><bdo id='loPyY'><pre id='loPyY'><center id='loPyY'></center></pre></bdo></b><th id='loPyY'></th></span></q></dt></tr></i><div id='loPyY'><tfoot id='loPyY'></tfoot><dl id='loPyY'><fieldset id='loPyY'></fieldset></dl></div>

      1. <tfoot id='loPyY'></tfoot>
      2. <legend id='loPyY'><style id='loPyY'><dir id='loPyY'><q id='loPyY'></q></dir></style></legend>

          <bdo id='loPyY'></bdo><ul id='loPyY'></ul>
      3. 您可以拆分/分解 MySQL 查询中的字段吗?

        时间:2023-08-18

          <i id='7Nndi'><tr id='7Nndi'><dt id='7Nndi'><q id='7Nndi'><span id='7Nndi'><b id='7Nndi'><form id='7Nndi'><ins id='7Nndi'></ins><ul id='7Nndi'></ul><sub id='7Nndi'></sub></form><legend id='7Nndi'></legend><bdo id='7Nndi'><pre id='7Nndi'><center id='7Nndi'></center></pre></bdo></b><th id='7Nndi'></th></span></q></dt></tr></i><div id='7Nndi'><tfoot id='7Nndi'></tfoot><dl id='7Nndi'><fieldset id='7Nndi'></fieldset></dl></div>
            <bdo id='7Nndi'></bdo><ul id='7Nndi'></ul>
          • <small id='7Nndi'></small><noframes id='7Nndi'>

          • <legend id='7Nndi'><style id='7Nndi'><dir id='7Nndi'><q id='7Nndi'></q></dir></style></legend>
            <tfoot id='7Nndi'></tfoot>
                    <tbody id='7Nndi'></tbody>

                  本文介绍了您可以拆分/分解 MySQL 查询中的字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我必须创建一份关于一些学生完成情况的报告.每个学生都属于一个客户.这是表格(针对此问题进行了简化).

                  I have to create a report on some student completions. The students each belong to one client. Here are the tables (simplified for this question).

                  CREATE TABLE  `clients` (
                    `clientId` int(10) unsigned NOT NULL auto_increment,
                    `clientName` varchar(100) NOT NULL default '',
                    `courseNames` varchar(255) NOT NULL default ''
                  )
                  

                  courseNames 字段包含以逗号分隔的课程名称字符串,例如AB01,AB02,AB03"

                  The courseNames field holds a comma-delimited string of course names, eg "AB01,AB02,AB03"

                  CREATE TABLE  `clientenrols` (
                    `clientEnrolId` int(10) unsigned NOT NULL auto_increment,
                    `studentId` int(10) unsigned NOT NULL default '0',
                    `courseId` tinyint(3) unsigned NOT NULL default '0'
                  )
                  

                  此处的courseId 字段是clients.courseNames 字段中课程名称的索引.因此,如果客户端的courseNames 是AB01,AB02,AB03",并且注册的courseId2,那么学生在AB03.

                  The courseId field here is the index of the course name in the clients.courseNames field. So, if the client's courseNames are "AB01,AB02,AB03", and the courseId of the enrolment is 2, then the student is in AB03.

                  有没有办法可以在这些表上进行单一选择,其中包括课程名称?请记住,会有来自不同客户的学生(因此具有不同的课程名称,并非所有课程名称都是连续的,例如:NW01,NW03")

                  Is there a way that I can do a single select on these tables that includes the course name? Keep in mind that there will be students from different clients (and hence have different course names, not all of which are sequential,eg: "NW01,NW03")

                  基本上,如果我可以拆分该字段并从结果数组中返回单个元素,那将是我正在寻找的.这是我在神奇伪代码中的意思:

                  Basically, if I could split that field and return a single element from the resulting array, that would be what I'm looking for. Here's what I mean in magical pseudocode:

                  SELECT e.`studentId`, SPLIT(",", c.`courseNames`)[e.`courseId`]
                  FROM ...
                  

                  推荐答案

                  直到现在,我都想在我的 SQL 数据库中保留那些逗号分隔的列表 - 非常了解所有警告!

                  Until now, I wanted to keep those comma separated lists in my SQL db - well aware of all warnings!

                  我一直认为它们比查找表(它提供了一种标准化数据库的方法)有好处.经过几天的拒绝,我看到了曙光:

                  I kept thinking that they have benefits over lookup tables (which provide a way to a normalized data base). After some days of refusing, I've seen the light:

                  • 在一个字段中使用逗号分隔值时,使用查找表不会产生比那些丑陋的字符串操作更多的代码.
                  • 查找表允许使用本机数字格式,因此不会比那些 csv 字段大.虽然它更小.
                  • 涉及的字符串操作在高级语言代码(SQL 和 PHP)中很少,但与使用整数数组相比成本很高.
                  • 数据库不应该是人类可读的,因为它们的可读性/直接可编辑性而试图坚持结构,这在很大程度上是愚蠢的,就像我所做的一样.

                  简而言之,MySQL 没有原生的 SPLIT() 函数是有原因的.

                  In short, there is a reason why there is no native SPLIT() function in MySQL.

                  这篇关于您可以拆分/分解 MySQL 查询中的字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL:ORDER BY RAND() 的替代方案 下一篇:将值从一个字段拆分为两个

                  相关文章

                  最新文章

                    <small id='enLZC'></small><noframes id='enLZC'>

                    <legend id='enLZC'><style id='enLZC'><dir id='enLZC'><q id='enLZC'></q></dir></style></legend>
                    <tfoot id='enLZC'></tfoot>
                  1. <i id='enLZC'><tr id='enLZC'><dt id='enLZC'><q id='enLZC'><span id='enLZC'><b id='enLZC'><form id='enLZC'><ins id='enLZC'></ins><ul id='enLZC'></ul><sub id='enLZC'></sub></form><legend id='enLZC'></legend><bdo id='enLZC'><pre id='enLZC'><center id='enLZC'></center></pre></bdo></b><th id='enLZC'></th></span></q></dt></tr></i><div id='enLZC'><tfoot id='enLZC'></tfoot><dl id='enLZC'><fieldset id='enLZC'></fieldset></dl></div>

                      <bdo id='enLZC'></bdo><ul id='enLZC'></ul>