如何添加新列并从其他列获取数据? SQL

时间:2018-06-14 14:17:12

标签: mysql sql database

您可以在下面的图片中看到我的意思。 我想创建新列“ Bill To This Company ”并从 BillToCustomerID

获取公司名称

enter image description here

4 个答案:

答案 0 :(得分:1)

你可能需要自我加入

select a.customerId, a.CustomerName, a.BillToCustomerId, b.CustmerName 
from my_table a 
inner join my_table  b on a.BillToCustomerId = b.customerId

答案 1 :(得分:1)

只需在表上自行加入并为该CustomerName提供别名

CREATE TABLE [dbo].[Table](
[Id] [uniqueidentifier] NOT NULL,
[BatchNumber] [nvarchar](max) NULL,
[JobType] [nvarchar](max) NULL,
[Operator] [nvarchar](max) NULL,
[TransactionNumber] [int] NULL,
[Status] [nvarchar](100) NULL,
[CreatedOn] [datetime] NOT NULL,
[ModifiedOn] [datetime] NOT NULL,

答案 2 :(得分:0)

似乎你需要subquery

select c.*,
       (select c1.CustomerName
        from Customer c1
        where c1.BillToCustomerId = c.BillToCustomerId
        order by c1.customerid 
        limit 1
       ) as BilltoThisCompany
from Customer c;

答案 3 :(得分:0)

<?php
$data = $_REQUEST;
$user_text = $_REQUEST['user_text'];
$response = strtoupper($user_text);
echo $response;
?>