简体   繁体   中英

combining multiple rows and join tables in sql query

first of all I want to thank everybody who takes the time to help others! This website is great and I already found some solutions for my problems. But now I'm stuck and I would like to ask for some help please. I'm working on a new project where I want to show a chart based on sql table values. It is about weight loss and my users input their data using a form. This datas are stored in 2 tables:

famd0_facileforms_records:

id    user_id    username
--------------------------
14    653        username1
15    648        username2

famd0_facileforms_subrecords:

id    record   element    title        name      type            value
------------------------------------------------------------------------------------
199   14       225        Datum        datum     Calendar        2012-08-22 11:32:07
200   14       226        Gewicht      gewicht   Text            92
201   14       242        BMI          bmi       Text            33.79
209   15       225        Datum        datum     Calendar        2012-08-01 11:53:05
210   15       226        Gewicht      gewicht   Text            83
212   15       242        BMI          bmi       Text            26.20 

I was able to show all the records in a chart by using the following sql query:

SELECT CONVERT(d1.value, DATE) AS Datum, CONVERT(d2.value, DECIMAL(5,1)) AS Gewicht  
FROM famd0_facileforms_subrecords AS d1  
INNER JOIN famd0_facileforms_subrecords AS d2 ON d1.record = d2.record  
WHERE d1.element = 225 AND d2.element = 226  
ORDER BY d1.value ASC  

But I want to have only the records of the logged user in the chart. I can use a variable for the logged in user, for example:

SELECT `id` FROM `famd0_facileforms_records` WHERE `user_id` = %%J_USER_ID%%

And this is my problem: I have no idea how to join both tables and show only the entries of the logged in user in my chart.

Any help would be great.

Thank's a lot! webnicole

It looks like you just need to do the following. You will add a JOIN to the table famd0_facileforms_records and place in the WHERE clause a filter on the user_id.:

SELECT x.id, 
    CONVERT(d1.value, DATE) AS Datum, 
    CONVERT(d2.value, DECIMAL(5,1)) AS Gewicht
FROM famd0_facileforms_subrecords AS d1
INNER JOIN famd0_facileforms_subrecords AS d2 
    ON d1.record = d2.record
LEFT JOIN famd0_facileforms_records x
    ON d1.record = x.id
WHERE d1.element = 225 
    AND d2.element = 226
    AND x.user_id = %%J_USER_ID%%
ORDER BY d1.value ASC

If you are getting an error but you still want to filter by id, you can add the WHERE clause and just not include the id in the SELECT :

SELECT CONVERT(d1.value, DATE) AS Datum, 
    CONVERT(d2.value, DECIMAL(5,1)) AS Gewicht
FROM famd0_facileforms_subrecords AS d1
INNER JOIN famd0_facileforms_subrecords AS d2 
    ON d1.record = d2.record
LEFT JOIN famd0_facileforms_records x
    ON d1.record = x.id
WHERE d1.element = 225 
    AND d2.element = 226
    AND x.user_id = %%J_USER_ID%%
ORDER BY d1.value ASC 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM