[英]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. 我正在研究一个新项目,该项目要显示基于sql表值的图表。 It is about weight loss and my users input their data using a form. 这与减肥有关,我的用户使用表格输入数据。 This datas are stored in 2 tables: 此数据存储在2个表中:
famd0_facileforms_records: famd0_facileforms_records:
id user_id username
--------------------------
14 653 username1
15 648 username2
famd0_facileforms_subrecords: 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: 通过使用以下sql查询,我能够在图表中显示所有记录:
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.: 您将在表famd0_facileforms_records
添加一个JOIN
,并在WHERE
子句中放置一个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
: 如果遇到错误,但仍要按ID进行过滤,则可以添加WHERE
子句,而不必在SELECT
包括该ID:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.