Advanced MySQL Joining. Speeding up query

后端 未结 3 767
春和景丽
春和景丽 2021-01-23 01:56

I have a list of users on my php application (using codeigniter). Each user may have completed a form with about 1000 or so total fields. The structure looks similar to this:

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 02:33

    I'm not sure I completely understand the problem. You can use a single query to get all users, and all of their fields (basically, this is what Philip suggested):

    SELECT u.username, f.* 
    FROM user AS u
        INNER JOIN completed_form_fields AS f
        ON f.userid = u.id
    ORDER BY u.id, f.fieldkey
    

    To filter the results, add a WHERE clause with the conditions. For example, to only get data from fieldkeys 'k1', 'k2' and 'k3':

    SELECT u.username, f.* 
    FROM user AS u
        INNER JOIN completed_form_fields AS f
        ON f.userid = u.id
    WHERE f.fieldkey IN ('k1', 'k2', 'k3')
    ORDER BY u.id, f.fieldkey
    

    Is this what you're looking for?

提交回复
热议问题