使用mysql_query在一个数组中添加两个表

时间:2013-06-21 12:27:34

标签: mysql arrays

我在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 =这是一个变量,用户将从输入字段输入。

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

1 个答案:

答案 0 :(得分:0)

假设adduct的内容固定为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);            



?>