将列值显示为表标题而不是其原始标题

时间:2012-03-24 18:08:53

标签: php mysql

我在mySQL中有一个数据库,让我们说“存储”,其中有一个名为“Storage_Det”的表。以下是“Storage_Det”的内容:

valueid | formid |  submissionid |  fieldname  |  fieldvalue
--------+--------+---------------+-------------+-------------
1       | 1      |  1            |  name       |  Alex
2       | 1      |  1            |  position   |  Manager
3       | 1      |  1            |  room       |  3-10
4       | 1      |  2            |  name       |  Ben
5       | 1      |  2            |  position   |  Accountant
6       | 1      |  2            |  room       |  2-05
7       | 1      |  3            |  name       |  Denny
8       | 1      |  3            |  position   |  Marketing
9       | 1      |  3            |  room       |  1-03

用php显示它没有问题。我的问题是:我想为我的新表格视图显示“名称”,“位置”和“房间”,以便它显示如下:

submissionid | name   | position   | room |
-------------+--------+------------+------+
1            | Alex   | Manager    | 3-10 |
2            | Ben    | Accountant | 2-05 |
3            | Denny  | Marketing  | 1-03 |

由于我是新手,我需要你的帮助。请告诉我。感谢。

好的,我想我必须先添加一些细节才能让我的问题足够清楚。

“表号2”不是真正的表。它是根据表编号1中存储的数据生成的。正如您在“表编号1”数据中看到的那样,“名称”,“位置”和“房间”将是“表编号2”中的“表头”。我希望这很清楚。

3 个答案:

答案 0 :(得分:1)

现在我更好地理解你的问题是一种方式:

CREATE TEMPORARY TABLE  `table2` (
`submissionid` INT NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
`position` VARCHAR( 50 ) NULL ,
`room` VARCHAR( 50 ) NULL
) ENGINE = MYISAM ;

ALTER TABLE  `table2` 
ADD UNIQUE (
`submissionid` ,
`name` ,
`position` ,
`room`
);

insert table2 (submissionid, name) select submissionid, fieldvalue from table1 where fieldname='name'; 
update table2 set position = (select fieldvalue from table1 where fieldname='position' and table1.submissionid = table2.submissionid); 
update  table2 set room = (select fieldvalue from table1 where fieldname='room' and table1.submissionid = table2.submissionid);   


select * from table2;

答案 1 :(得分:0)

首先,将所有数据分组为某个submissionid,如此(假设非常基本的mysql API):

$sIdGroup = array();
while ( $row = mysql_fetch_assoc( $result ) )
{
    if ( !isset( $sIdGroup[ $row[ "submissionid" ] ] ) )
    {
        $sIdGroup[ $row[ "submissionid" ] ] = array();
    }

    $sIdGroup[ $row[ "submissionid" ] ][ $row[ "fieldname" ] ] = $row[ "fieldvalue" ];  // Save the value for the current field
}

现在显示,首先是列名:

echo( '<table><tr><td>submissionid</td>' );

// Get the first row in $sIdGroup (i'm assuming 1 is not always the first index)

reset( $sIdGroup );
$firstSubmissionId = key( $sIdGroup );

foreach ( $sIdGroup[ $firstSubmissionId ] as $key => $dummy )
{
    echo( "<td>$key</td>" );
}

现在数据:

echo( '</tr>' );

foreach ( $sIdGroup as $submissionId => $data )
{
    echo( "<tr><td>$submissionId</td>" );
    foreach ( $data as $key => $value )
    {
        echo( "<td>$value</td>" );
    }
    echo( '</tr>' );
}

echo( '</table>' );

答案 2 :(得分:0)

如果您希望查询向您显示您所请求的数据,可以使用以下内容:

SELECT DISTINCT sd.SubmissionId
   , sdName.FieldValue AS Name
   , sdPosition.FieldValue AS Position
   , sdRoom.FieldValue AS Room
FROM Storage_Det AS sd
LEFT JOIN Storage_Det AS sdName ON sd.SubmissionId = sdName.SubmissionId
   AND sdName.FieldName = 'Name'
LEFT JOIN Storage_Det AS sdPosition ON sd.SubmissionId = sdPosition.SubmissionId
   AND sdPosition.FieldName = 'Position'
LEFT JOIN Storage_Det AS sdRoom ON sd.SubmissionId = sdRoom.SubmissionId
   AND sdRoom.FieldName = 'Room'