0

我在 mysql 中有两个表,我们称它们为脂质和加合物:

脂质:

    ID  |  name  |  mz  |  hg  |
    1   |  36:2  |  785 |  PC  |
    2   |  36:1  |  803 |  PT  |
    3   |  36:2  |  804 |  PS  |

加合物:

    ID  |  name  |  mz     |  charge   |
    1   |  +H+   |  1.     |  1        |
    2   |  -H+   |  -1.    |  -1       |

好的,我需要搜索 36:2,但我需要提供所有信息,另外我需要将加合物表添加到 mz。所以我需要得到一个像这样的数组:

    [ID] => 1
    [name] => 36:2
    [hg] => PC
    [mz] => 785
    [mz +H+]=>786
    [charge +H+] =>1
    [mz-H+] =>784
    [charge -H+] =>-1

    [ID] => 3
    [name] => 36:2
    [hg] => PS
    [mz] => 804
    [mz +H+]=>805
    [charge +H+] =>1
    [mz-H+] =>803
    [charge -H+] =>-1

编辑:

我正在使用一个网站从 mysql 数据库中获取结果......所以我正在使用这个 php 代码......来获取一些结果。

if(preg_match('/([0-9]+):([0-9]+)/',$name))
{
 if(!$head)
   {
       $result = mysql_query("SELECT  * FROM lipids where name='$name'");
   }

  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
     {
       print_r($row);            
    }

//$name = 这是一个变量,用户将从输入字段中输入。

oen 输入会有多个答案。所以答案列表会很大。上面的表格只是一个快速的概述,因为它们会有更多的数据。

4

1 回答 1

0

假设加合物的内容固定在 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);            



?>
于 2013-06-21T13:14:18.163 回答