| | |
Complex query returning one row from multiple rows
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
Hello all. Thanks for the help!
Two tables:
Table `posts` holds 'p_id', 'post', 'u_id'
Table `users_profile` holds 'u_id', 'profile_key', 'value'
How do I return one row for each 'p_id' that includes all the information in the `posts` table as well as the information in the `users_profile` table WHERE `posts`.`u_id` = `users_profile`.`u_id` ?
I would also like for the 'profile_key' to become the key for the 'value' when I use $row = mysql_fetch_assoc($r); in my php script.
The problem is that `u_id` in the `users_profile` table is not unique. The table is structured like:
Hope someone can help me. I know it would be simpler to structure the users_profile table with one row for each 'u_id' but I like the idea of doing it as shown above so there would be a potential to have unlimited profile_keys for every user.
p_id = "post id"
u_id = "user id"
Thanks a lot. I have learned a lot from these forums. They are great!
Two tables:
Table `posts` holds 'p_id', 'post', 'u_id'
Table `users_profile` holds 'u_id', 'profile_key', 'value'
How do I return one row for each 'p_id' that includes all the information in the `posts` table as well as the information in the `users_profile` table WHERE `posts`.`u_id` = `users_profile`.`u_id` ?
I would also like for the 'profile_key' to become the key for the 'value' when I use $row = mysql_fetch_assoc($r); in my php script.
The problem is that `u_id` in the `users_profile` table is not unique. The table is structured like:
u_id profile_key value 1 first_name Jason 1 last_name Smith 2 first_name Sam 1 birth_date 01/01/1970 2 etc etc
Hope someone can help me. I know it would be simpler to structure the users_profile table with one row for each 'u_id' but I like the idea of doing it as shown above so there would be a potential to have unlimited profile_keys for every user.
p_id = "post id"
u_id = "user id"
Thanks a lot. I have learned a lot from these forums. They are great!
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
MySQL Syntax (Toggle Plain Text)
SELECT p.p_id, post, p.u_id, u.profile_key, u.value FROM posts p INNER JOIN users_profile u on p.u_id = u.u_id
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
sorry forgot to look at your row structure, give me a few and i'll post back
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
Yeah, this one is tough. I hope you can figure it out.
Here is the structure:
-- Structure de la table `users_profile`
--
Problem is when you have ONE post with a u_id, but you have many rows in the users_profile table with the same u_id.
So even though I only want one row with one post, I get a bunch of rows because the u_id appears in a bunch of rows of the users_profile table.
I know this is a pretty advanced query, so I really appreciate the help.
Here is the structure:
-- Structure de la table `users_profile`
--
MySQL Syntax (Toggle Plain Text)
CREATE TABLE IF NOT EXISTS `users_profile` ( `u_id` INT(10) UNSIGNED NOT NULL, `profile_key` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `profile_value` TEXT COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`u_id`,`profile_key`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- Contraintes pour les tables exportées -- -- -- Contraintes pour la table `users_profile` -- ALTER TABLE `users_profile` ADD CONSTRAINT `users_profile_ibfk_1` FOREIGN KEY (`u_id`) REFERENCES `users` (`u_id`);
Problem is when you have ONE post with a u_id, but you have many rows in the users_profile table with the same u_id.
So even though I only want one row with one post, I get a bunch of rows because the u_id appears in a bunch of rows of the users_profile table.
I know this is a pretty advanced query, so I really appreciate the help.
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
are you able to use a stored procedure, or must it be a select only?
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
working on a single select right now rather than a proc, i'll see how much time i have to finish
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
DELIMITER $$
CREATE PROCEDURE `p_GetUser`(v_U_ID int)
BEGIN
select u_id,
(select value from users_profile where profile_key = 'first_name' and u_id = v_U_ID) as first_name,
(select value from users_profile where profile_key = 'last_name' and u_id = v_U_ID) as last_name,
(select value from users_profile where profile_key = 'birth_date' and u_id = v_U_ID) as birth_date
from users_profile
where
u_id = v_U_ID
group by u_id;
end
CREATE PROCEDURE `p_GetUser`(v_U_ID int)
BEGIN
select u_id,
(select value from users_profile where profile_key = 'first_name' and u_id = v_U_ID) as first_name,
(select value from users_profile where profile_key = 'last_name' and u_id = v_U_ID) as last_name,
(select value from users_profile where profile_key = 'birth_date' and u_id = v_U_ID) as birth_date
from users_profile
where
u_id = v_U_ID
group by u_id;
end
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
here's a select, just add where u_id = ?
MySQL Syntax (Toggle Plain Text)
SELECT u_id, max(if(profile_key='first_name', VALUE, NULL)) as first_name, max(if(profile_key='last_name', VALUE, NULL)) as last_name, max(if(profile_key='birth_date', VALUE, NULL)) as birth_date, max(if(profile_key='etc', VALUE, NULL)) as birth_date FROM users_profile GROUP BY u_id;
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Other Threads in the MySQL Forum
- Previous Thread: MYSQL SERVER!!!! does not log in
- Next Thread: MySQL Distributed Database
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright court crm data database design developer development distinct dui eliminate enter enterprise error eudora facebook form foss gartner gnu government gpl greenit groupware hiring hyperic images innerjoins insert ip joebrockmeier join keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron microsoft microsoftexchange mindtouch multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opengovernment opensource operand oracle penelope php priceupdating query referencedesign reorderingcolumns saas search select sharepoint simpledb spotify statement sugarcrm syntax techsupport thunderbird transparency update virtualization






