如何从单列创建两列输出 [英] How to create two column output from a single column

查看:76
本文介绍了如何从单列创建两列输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜这是一个菜鸟问题,所以请多多包涵.我的mySQL表中有一列,其中包含名字和姓氏以及其他数据(请参见下表).

I am guessing this is a noob question so please bear with me. I have a column in a mySQL table that contains both first name and last name plus other data (see table below).

fid | uid | value
 5  |  1  | John
 6  |  1  | Doe
 7  |  1  | some other data
 5  |  2  | Jane
 6  |  2  | Doe
 7  |  2  | some other data

我想做的是创建一个查询,在其中我将名字和姓氏分成各自的列以进行报告(如下所示).

What I would like to do is create a query where I split out the first and last names into their own columns for reporting purposes (like shown below).

First Name | Last Name
  John     |   Doe
  Jane     |   Doe

在此之前,我还没有看到这个问题,也没有能够使用Google(也许使用了错误的关键字).我以为这是相对简单的,但却使我难以理解.

I haven't seen this question asked before here nor have I been able to Google (perhaps using the wrong keywords). I assume this is relatively simple but it is eluding me.

谢谢.

推荐答案

您只需要使用以下方法将表连接到自身:

You just need to join the table to itself with something like this:

select first_name.value, last_name.value
from your_table first_name
join your_table last_name on first_name.uid = last_name.uid
where first_name.fid = 5
  and last_name.fid  = 6

只要您对表的每个实例使用不同的别名,就可以将表自身连接或多次连接同一表.

You can join a table to itself or join the same table multiple times as long as you use a different alias for each instance of the table.

这篇关于如何从单列创建两列输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆