我有以下查询:帮助MySQL查询
SELECT u.uid, pf.title, pv.value FROM users AS u INNER JOIN
profile_values AS pv ON pv.uid = u.uid
INNER JOIN
profile_fields AS pf ON pf.fid = pv.fid
ORDER BY u.uid
导致:
uid title value
1 First name Jared
1 Last name Boo
1 Organization Acme
1 Website http://acme.com
1 Country Canada
6 First name Nathan
6 Last name Foo
我试图用这个结果来创建另一个结果集,它看起来是这样的:
uid First name Last name Organization Website Country
1 Jared Boo Acme http... Canada
6 Nathan Foo
我正确地处理这个问题吗?
在此先感谢
编辑
表:
CREATE TABLE `users` (
`uid` int(10) unsigned NOT NULL auto_increment,
`name` varchar(60) NOT NULL default '',
`pass` varchar(32) NOT NULL default '',
`mail` varchar(64) default '',
`created` int(11) NOT NULL default '0',
`access` int(11) NOT NULL default '0',
PRIMARY KEY (`uid`),
UNIQUE KEY `name` (`name`),
KEY `access` (`access`),
KEY `created` (`created`),
KEY `mail` (`mail`)
);
CREATE TABLE `profile_fields` (
`fid` int(11) NOT NULL auto_increment,
`title` varchar(255) default NULL,
`name` varchar(128) NOT NULL default '',
`explanation` text,
`category` varchar(255) default NULL,
PRIMARY KEY (`fid`),
UNIQUE KEY `name` (`name`),
KEY `category` (`category`)
);
CREATE TABLE `profile_values` (
`fid` int(10) unsigned NOT NULL default '0',
`uid` int(10) unsigned NOT NULL default '0',
`value` text,
PRIMARY KEY (`uid`,`fid`),
KEY `fid` (`fid`)
);
当一个用户有多个名字时会发生什么? – 2010-12-14 22:17:50
另外,你打算动态生成列标题,还是在查询中定义它应该提取哪些字段? – 2010-12-14 22:18:54
用户不会有任何一个字段。对于我来说,使用“标题”的值作为列标题似乎很合理 – 2010-12-14 22:22:13