简体   繁体   English

从3个MySQL表中获取数据

[英]Getting data from 3 MySQL tables

I'm having a struggle here trying to grab some data from 3 database tables.. 我在这里有一个斗争试图抓住从3个数据库表中的一些数据..

The idea is that users can fill out their profile with several fields, and I'm storing every profile field, field values and the users selected value in separate tables. 这个想法是用户可以用几个字段来填充他们的配置文件,并且我将每个配置文件字段,字段值和用户选择的值存储在单独的表中。

The structure of the tables look like this: 表的结构如下所示:

Table 'profile_fields'
- id
- name
- sort
- status (enum '0', '1')

Table 'profile_field_values'
- id
- profile_field_id
- name

Table 'user_profile_fields'
- user_id
- profile_field_id
- profile_field_value_id

Would be really nice if you could tell me how to construct this query, and why you used the JOIN you did. 如果您能告诉我如何构造此查询以及为什么使用所做的JOIN,那将是非常不错的。

Also, how would this table layout scale when the userbase grows? 另外,当用户群增加时,此表布局将如何缩放?

Thank you so much in advance! 提前非常感谢您!

Edit: OK, I still can't figure out how to make it return all the fields from 'profile_fields' along with the users selected option from 'user_profile_fields'. 编辑:好的,我仍然不知道如何使它从'profile_fields'返回所有字段以及从'user_profile_fields'中选择的用户选项。 If the user hasn't selected a value, it should just be null. 如果用户尚未选择值,则该值应为null。

This is my (non-functional) query so far: 到目前为止,这是我的(非功能性)查询:

SELECT  PF.id AS field_id, PF.name AS field_name, UPF.profile_field_value_id AS value_id, PF.type, PFV.name 
FROM  profile_fields  PF
LEFT JOIN profile_fields_values PFV ON PFV.profile_field_id = PF.id
LEFT JOIN user_profile_fields UPF ON UPF.user_id=1  AND PF.id = UPF.profile_field_id
WHERE length(PF.name) > 0 and PF.status = '1'
ORDER BY PF.sort

This query seems to work, but it does not fetch the name of the value from 'profile_field_values': 该查询似乎有效,但是它不会从“ profile_field_values”中获取值的名称:

SELECT PF.id AS field_id, PF.name AS field_name, UPF.profile_field_value_id AS value_id, PF.type
FROM profile_fields PF
LEFT JOIN user_profile_fields UPF ON UPF.user_id =1
AND PF.id = UPF.profile_field_id
WHERE LENGTH( PF.name ) >0
AND PF.status =  '1'
ORDER BY PF.sort

I think you have some unnecessary complexity in there. 我认为您那里有些不必要的复杂性。 Maybe you should try 也许你应该尝试

Table 'profile_fields' 表“ profile_fields”

  • id ID
  • name 名称
  • sort 分类
  • status (enum '0', '1') 状态(枚举“ 0”,“ 1”)

Table 'profile_field_values' 表“ profile_field_values”

  • id ID
  • user_id 用户身份
  • profile_field_id profile_field_id
  • value

why are there 3 tables? 为什么有3张桌子?

Seems like simple JOINs should work: 似乎简单的JOIN应该起作用:

SELECT pf.id, pf.name, pf.sort, pf.status,
    pfv.id, pfv.profile_field_id, pfv.name,
    upf.user_id, upf.profile_field_id, upf.profile_field_value_id
FROM profile_fields pf
    INNER JOIN profile_field_values pfv 
        ON pf.id = pfv.profile_field_id
    INNER JOIN user_profile_fields upf 
        ON upf.profile_field_value_id = pfv.id AND upf.profile_field_id = pf.id

A Visual Explanation of SQL Joins SQL连接的直观说明

This uses an INNER JOIN to select all matching records from each table -- review the post to tell the difference between an INNER and an OUTER join. 这使用INNER JOIN从每个表中选择所有匹配的记录-查看该帖子以了解INNEROUTER连接之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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