[英]Merge multiple rows to one
I have the following query, but this one returns 3 rows and i want one row ;-) 我有以下查询,但此查询返回3行,我要一行;-)
SELECT
b_firstname ,value
FROM
cscart_user_profiles
RIGHT JOIN profile_fields_data ON profile_fields_data.object_id = user_profiles.profile_id
WHERE
user_profiles.b_title NOT LIKE ''
AND user_profiles.profile_id = '4252'
AND (
profile_fields_data.field_id ='69'
OR
profile_fields_data.field_id ='73'
OR
profile_fields_data.field_id ='75'
)
... but this will return 3 rows: ...但是这将返回3行:
user1 value
user1 value
user1 value
I want 1 row: 我想要1行:
user1 value69 user73 value75 用户1值69用户73值75
How can I solve this? 我该如何解决?
Below the 2 tables where the data is 在2个表格下方,数据是
CREATE TABLE IF NOT EXISTS `cscart_user_profiles` (
`profile_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`user_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`profile_type` char(1) NOT NULL DEFAULT 'P',
`b_title` varchar(32) NOT NULL DEFAULT '',
`b_firstname` varchar(128) NOT NULL DEFAULT '',
`b_lastname` varchar(128) NOT NULL DEFAULT '',
`b_address` varchar(64) NOT NULL DEFAULT '',
`b_address_2` varchar(64) NOT NULL DEFAULT '',
`b_city` varchar(64) NOT NULL DEFAULT '',
`b_county` varchar(32) NOT NULL DEFAULT '',
`b_state` varchar(32) NOT NULL DEFAULT '',
......
PRIMARY KEY (`profile_id`),
KEY `uid_p` (`user_id`,`profile_type`),
KEY `profile_type` (`profile_type`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
CREATE TABLE IF NOT EXISTS `cscart_profile_fields_data` (
`object_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`object_type` char(1) NOT NULL DEFAULT 'U',
`field_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`value` varchar(255) NOT NULL DEFAULT '0',
PRIMARY KEY (`object_id`,`object_type`,`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
You could join to the profile_fields_data table 3 separate times to get the 3 separate values on a single row. 您可以单独连接profile_fields_data表3次,以在一行中获得3个独立的值。
SELECT b_firstname,
pfd69.value as value69,
pfd73.value as value73,
pfd75.value as value75
FROM cscart_user_profiles AS up
RIGHT JOIN cscart_profile_fields_data AS pfd69
ON pfd69.object_id = up.profile_id
AND pfd69.field_id ='69'
RIGHT JOIN cscart_profile_fields_data AS pfd73
ON pfd73.object_id = up.profile_id
AND pfd73.field_id ='73'
RIGHT JOIN cscart_profile_fields_data AS pfd75
ON pfd75.object_id = up.profile_id
AND pfd75.field_id ='75'
WHERE up.b_title NOT LIKE ''
AND up.profile_id = '4252'
Can you use nested queries? 您可以使用嵌套查询吗?
SELECT * FROM (
SELECT User1, Value69 FROM table ) AS T1
INNER JOIN (
SELECT User2, Value75 FROM table ) AS T2 ON T1.Somevalue = T2.Somevalue
etc. 等等
But without seeing your schema it's hard to write a helpful example 但是如果看不到您的架构,就很难写出有用的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.