Complex query returning one row from multiple rows

Reply

Join Date: Nov 2008
Posts: 7
Reputation: JasonDFR is an unknown quantity at this point 
Solved Threads: 0
JasonDFR JasonDFR is offline Offline
Newbie Poster

Complex query returning one row from multiple rows

 
0
  #1
Nov 13th, 2008
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:
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Complex query returning one row from multiple rows

 
0
  #2
Nov 13th, 2008
  1. SELECT p.p_id, post, p.u_id, u.profile_key, u.value
  2. FROM posts p
  3. INNER JOIN users_profile u
  4. on p.u_id = u.u_id
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Complex query returning one row from multiple rows

 
0
  #3
Nov 13th, 2008
sorry forgot to look at your row structure, give me a few and i'll post back
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: JasonDFR is an unknown quantity at this point 
Solved Threads: 0
JasonDFR JasonDFR is offline Offline
Newbie Poster

Re: Complex query returning one row from multiple rows

 
0
  #4
Nov 13th, 2008
Yeah, this one is tough. I hope you can figure it out.

Here is the structure:

-- Structure de la table `users_profile`
--
  1. CREATE TABLE IF NOT EXISTS `users_profile` (
  2. `u_id` INT(10) UNSIGNED NOT NULL,
  3. `profile_key` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `profile_value` TEXT COLLATE utf8_unicode_ci NOT NULL,
  5. PRIMARY KEY (`u_id`,`profile_key`)
  6. ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  7.  
  8. --
  9. -- Contraintes pour les tables exportées
  10. --
  11.  
  12. --
  13. -- Contraintes pour la table `users_profile`
  14. --
  15. ALTER TABLE `users_profile`
  16. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Complex query returning one row from multiple rows

 
0
  #5
Nov 13th, 2008
are you able to use a stored procedure, or must it be a select only?
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: JasonDFR is an unknown quantity at this point 
Solved Threads: 0
JasonDFR JasonDFR is offline Offline
Newbie Poster

Re: Complex query returning one row from multiple rows

 
0
  #6
Nov 13th, 2008
A stored procedure would be fine.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Complex query returning one row from multiple rows

 
0
  #7
Nov 13th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Complex query returning one row from multiple rows

 
0
  #8
Nov 13th, 2008
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
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Complex query returning one row from multiple rows

 
0
  #9
Nov 13th, 2008
here's a select, just add where u_id = ?

  1. SELECT u_id,
  2. max(if(profile_key='first_name', VALUE, NULL)) as first_name,
  3. max(if(profile_key='last_name', VALUE, NULL)) as last_name,
  4. max(if(profile_key='birth_date', VALUE, NULL)) as birth_date,
  5. max(if(profile_key='etc', VALUE, NULL)) as birth_date
  6. FROM users_profile
  7. GROUP BY u_id;
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: JasonDFR is an unknown quantity at this point 
Solved Threads: 0
JasonDFR JasonDFR is offline Offline
Newbie Poster

Re: Complex query returning one row from multiple rows

 
0
  #10
Nov 13th, 2008
I'll try to get it working tomorrow morning. It's after 10PM here and I have been in front of the computer all day.

I really need to learn the theory behind these kinds of queries.

Thank you very much and perhaps we can talk more later.

Take care,

Jason
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MySQL Forum
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC