在一行中获得2行值的结果

时间:2018-07-11 13:16:51

标签: mysql sql string-aggregation

联接查询的结果-

SELECT st_stock.name, st_stock.price, 
       FORMAT(st_stock.quantity,0), 
       st_stock.exp_date, 
       admin.username 
FROM `st_stock` 
  INNER JOIN `admin` ON FIND_IN_SET(admin.ID,st_stock.broker_id) AND st_stock.id='2'

样本数据

name        price     asjdb       exp_date            username
5.HK (HSBC) 74.40 HKD 100,000,000 2018-07-27 17:00:00 broker2
5.HK (HSBC) 74.40 HKD 100,000,000 2018-07-27 17:00:00 broker3

预期输出

 5.HK (HSBC) 74.40 HKD 100,000,000 2018-07-27 17:00:00 broker2,broker3

2 个答案:

答案 0 :(得分:0)

您未指定要使用的SQL引擎。当涉及到更高级的聚合和窗口功能时,不同数据库引擎的实现有所不同。

例如在PostgreSQL中,您将使用string_agg聚合函数:

SELECT name, price, asjdb, exp_date, string_agg(username,',') AS username
FROM d
GROUP BY name, price, asjdb, exp_date;

对于其他数据库引擎,您将使用:

  1. Oracle:LISTAGG
  2. SQL Server:STRINGAGG
  3. MySQL:GROUP_CONCAT

进一步阅读: ListAGG in SQLSERVER

答案 1 :(得分:0)

如果是sql

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem /* Retrieve total physical memory: */
for /F "skip=1 delims=" %%I in ('wmic ComputerSystem get TotalPhysicalMemory') do for /F %%J in ("%%I") do set "TOTMEM=%%J"
echo total physical memory = %TOTMEM%

rem /* Retrieve total memory capacity: */
set "GBYTESUM=" & set "BYTESUM="
for /F "skip=1 delims=" %%I in ('wmic MemoryChip get Capacity') do for /F %%J in ("%%I") do (
    rem // Split huge number into two parts, Gigabyte and remaining Byte values:
    set "SIZE=%%J" & set "GBYTES=!SIZE:~,-9!" & set "SIZE=000000000%%J" & set "BYTES=!SIZE:~-9!"
    rem // Sum up Byte values, compensate for trailing zeros:
    set /A "BYTESUM+=1!BYTES!-1000000000"
    rem // Determine carry to be added to GigaByte values:
    set "CARRY=!BYTESUM:~,-9!" & set "CARRY+=0" & set "BYTESUM=!BYTESUM:~-9!"
    rem // Sum up Gigabyte values plus carry:
    set /A "GBYTESUM+=GBYTES+CARRY"
)
rem // Rejoin Gigabyte and Byte values into a huge number:
set "BYTESUM=000000000%BYTESUM%"
for /F "tokens=* delims=0 eol=0" %%Z in ("%GBYTESUM%%BYTESUM:~-9%") do set "TOTCAP=%%Z"
if not defined TOTCAP set "TOTCAP=0"
echo total memory capacity = %TOTCAP%

endlocal
exit /B