假设加合物的内容固定在 2 行,那么可能是这样的:-
SELECT a.ID, a.name, a.hg, a.mz, a.mz+b.mz AS 'mz +H+', b.mz AS 'charge +H+', a.mz+c.mz AS 'mz -H+', c.mz AS 'charge -H+'
FROM lipids a
LEFT JOIN adduct b ON b.name = '+H+'
LEFT JOIN adduct b ON b.name = '-H+'
但我不知道你是否会在加合物上出现许多 +H+ 行(例如)
编辑
您可以只进行一次连接并为每个 mz 名称和费用获取一行,然后在代码中对其进行排序(大多数语言都很容易):-
SELECT a.ID, a.name, a.hg, a.mz, b.name, a.mz+b.mz, b.mz
FROM lipids a
CROSS JOIN adduct b
WHERE a.name='36:2'
您还可以将所有数据连接到一个字段中,然后在代码中将其分解为一个数组。在php中很简单。
SELECT a.ID, a.name, a.hg, a.mz, GROUP_CONCAT(CONCAT_WS('~', 'mz', b.name, a.mz+b.mz, 'charge', b.name, b.mz) SEPARATOR '~')
FROM lipids a
CROSS JOIN adduct b
WHERE a.name='36:2'
GROUP BY a.ID, a.name, a.hg, a.mz
再次编辑
PHP 来完成这项工作
<?php
if(preg_match('/([0-9]+):([0-9]+)/',$name))
{
if(!$head)
{
$result = mysql_query("SELECT a.ID, a.name, a.hg, a.mz, b.name AS AdductName, a.mz+b.mz AS MzCharge, b.charge
FROM lipids a
CROSS JOIN adduct b
WHERE a.name='$name'");
$StoreArray = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$StoreArray['ID'] = $row['ID'];
$StoreArray['name'] = $row['name'];
$StoreArray['hg'] = $row['hg'];
$StoreArray['mz'] = $row['mz'];
$StoreArray['mz '.$row['AdductName']] = $row['MzCharge'];
$StoreArray['charge '.$row['AdductName']] = $row['charge'];
}
print_r($StoreArray);
}
}
?>
再次编辑
应对多条同名血脂记录
<?php
$link = mysql_connect('localhost', 'root', '');
If (!$link)
{
die ('Could not connect: ' . mysql_error());
}
@mysql_select_db('testarea') or die ('Unable to select database');
$name = '36:2';
$result = mysql_query("SELECT a.ID, a.name, a.hg, a.mz, b.name AS AdductName, a.mz+b.mz AS MzCharge, b.charge
FROM lipids a
CROSS JOIN adduct b
WHERE a.name='$name'");
$CurrId = 0;
$StoreArray = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($CurrId != $row['ID'])
{
$CurrId = $row['ID'];
$StoreArray[$CurrId] = array();
}
$StoreArray[$CurrId]['ID'] = $row['ID'];
$StoreArray[$CurrId]['name'] = $row['name'];
$StoreArray[$CurrId]['hg'] = $row['hg'];
$StoreArray[$CurrId]['mz'] = $row['mz'];
$StoreArray[$CurrId]['mz '.$row['AdductName']] = $row['MzCharge'];
$StoreArray[$CurrId]['charge '.$row['AdductName']] = $row['charge'];
}
print_r($StoreArray);
?>