简体   繁体   English

Mysql将列分为3列以在phpMyAdmin中查看

[英]Mysql split column into 3 columns for view in phpMyAdmin

I'm extremely new to writing SQL queries - I am hoping to create some charts in a front end application, but have to manipulate the data to create a view because the front end is not well suited to running complicated queries. 我对编写SQL查询非常陌生-我希望在前端应用程序中创建一些图表,但由于前端不适合运行复杂的查询,因此必须操纵数据以创建视图。

Here is my current situation: I have a table that has client data as well as a date that record was created. 这是我目前的情况:我有一个表,其中包含客户数据以及记录的创建日期。 Here is a sample not in any particular order. 这是一个没有特定顺序的示例。

|  ID  |   post_date   |    post_title    |
-------------------------------------------
| 1654 |  2017-09-04   | Bill Smith (5678)|
| 1658 |  2017-09-05   | Jan Jones (3423) |
| 1878 |  2017-08-17   | Jim Tanz (7890)  |
| 1659 |  2017-09-06   | Jan Jones (3425) |

I would like to display unique values by last name, but at the moment all the names are in one column. 我想按姓氏显示唯一值,但目前所有名称都在一栏中。 The ID is unique as it is incremented for each record and the number in parentheses (transaction ID) appended to the last name is also unique and comes from another application we are pulling the name from. 该ID是唯一的,因为它为每条记录递增,并且附加在姓氏上的括号中的数字(事务ID)也是唯一的,并且来自另一个我们要从中提取该名称的应用程序。

I have been able to split the post_title column, but only into 2 columns but am left with FName and LastName (TrID), which doesn't allow me to pick distinct entries by last name to do a client count because the TrIDs are all different. 我已经能够将post_title列拆分为两列,但是只剩下FName和LastName(TrID),这不允许我按姓氏选择不同的条目来进行客户计数,因为TrID都是不同的。

My intent was to create a view with 3 columns then display distinct entries by last name and count the clients, each month to see if there has been any client growth, but I am still at the very early step. 我的意图是创建一个包含3列的视图,然后按姓氏显示不同的条目并统计客户数量,每月查看客户数量是否有所增长,但我仍处于起步阶段。

Any assistance would be greatly appreciated (and remembered forever :>) Thanks! 任何帮助将不胜感激(并永远记住:>)谢谢!

Some text operations and it may work: 一些文本操作,它可能会起作用:

SELECT t.post_title
      ,LEFT(t.post_title, LOCATE(' ', post_title )) AS FName
      ,SUBSTR(t.post_title, LOCATE(' ', post_title)+1, LOCATE(' ',post_title,LOCATE(' ', post_title)+1)-LOCATE(' ', post_title)) AS LName
      ,REPLACE(REPLACE(TRIM(RIGHT(t.post_title,LOCATE(' ', REVERSE(post_title)))), '(', ''), ')','') AS ID
FROM (SELECT 'Bill Smith (5678)' AS post_title
      UNION SELECT 'Jan Jones (3423)'
      UNION SELECT 'Jim Tanz (7890)') t;

Rextester Demo Rextester演示

You can use SUBSTRING_INDEX to separate the string, so to retrieve the first name: 您可以使用SUBSTRING_INDEX来分隔字符串,以便检索名字:

SUBSTRING_INDEX(post_title," ",1)

This gets everything up until the nth instance of the space, so it's a bit messier to get the last name, as when using '2' we will get the values up until the second space, then we need to then extract the second value (-1, as we go backwards). 这样可以将所有内容累积到该空间的第n个实例,因此获取姓氏有点麻烦,因为使用'2'时,我们将获得直到第二个空格的值,然后需要提取第二个值( -1,因为我们向后走)。 Therefore, getting the 'Last Name' is done using: 因此,使用以下命令获取“姓氏”:

SUBSTRING_INDEX(SUBSTRING_INDEX(post_title," ",2)," ",-1)

Scenario 1: Splitting post_title into three fields: 方案1:将 post_title分为三个字段:

SELECT
SUBSTRING_INDEX(post_title," ",1) as firstName, 
SUBSTRING_INDEX(SUBSTRING_INDEX(post_title," ",2)," ",-1) as lastName,
SUBSTRING_INDEX(REPLACE(REPLACE(post_title,"(",""),")","")," ",-1) as post_ID
FROM tableName;

Output: 输出:

+-----------+----------+---------+
| firstName | lastName | post_ID | 
---------------------------------+
| Bill      | Smith    | 5678    | 
| Jan       | Jones    | 3423    | 
| Jim       | Tanz     | 7890    | 
| Jan       | Jones    | 3425    | 
+-----------+----------+---------+

Scenario 2: Grouping functions 方案2:分组功能

You could also use the named field to group and count by Last Name 您也可以使用命名字段对姓氏进行分组和计数

SELECT
COUNT(*) as Qty,
SUBSTRING_INDEX(SUBSTRING_INDEX(post_title," ",2)," ",-1) as lastName
FROM tableName
GROUP BY lastName;

Output: 输出:

+-----+----------+
| Qty | lastName |
+-----+----------+
|  2  | Jones    |
|  1  | Smith    | 
|  1  | Tanz     |
+-----+----------+

And so on. 等等。 Hard to tailor this any further, as I'm not fully sure what you're intending to do, but hopefully the above is of use. 由于我不确定您打算做什么,因此很难再对此进行调整,但是希望以上内容可以使用。

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

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