Results 1 to 6 of 6

Thread: "inner query"' only option for a left join that needs to match data from two tables?

  1. #1
    Join Date
    Nov 2010

    "inner query"' only option for a left join that needs to match data from two tables?

    lets assume I got three tables:
    users (ID, name)
    fields (ID, title)
    entries (nameid,titleid,value)

    I want an record back for every (users * fields) (with the corresponding value in 'entries' table)... So if 10 users, 10 fields, id expect 100 results back, each result with their corresponding 'value' in entries if any (null if none). but the curve ball: there may not be a record in the 'entries' table (so if a new field is added, every single user would not have that field in entries yet).

    This wont work:
    select mythings from users, fields left join entries on entries.titleID = fields.id /* this would not account for the userID matched to users.ID  */

    My effective problem is since there might not be a record in 'entries' I need to do a left join with entries, but id need data from both users and fields tables. This is my 'solution':
    select * from 
    (select mythings from users, fields) as T
    left join entries ON T.ID = entries.nameid and T.ID = entries.titleID

    Is there a better way to do this? Or is this the accepted solution to this?

  2. #2
    Join Date
    Nov 2010
    bump? This isnt working that great...

    I got over 400 users and 10 fields. That means I got over 4000 records in 'entries' table. Getting all clients data on a single page takes over 10 seconds. Im sure there got to be a better way.

    In the meantime I coded it so it limits to max of 50 users per page and it still takes few seconds (and in this situation 50 users per page is not ideal).

    Here is the exact query im using:
    select * from (
    	select clients.ID as 'userID',
    	    fields.ID as 'PKID',
    	from fields, clients
    	where fields.archived = 0
    	order by $sort, clients.ID, fields.ID asc
    	) as tt left joinclientextra on (tt.userID = clientExtra.clientID and tt.PKID = clientExtra.fieldID)

  3. #3
    Will this work

  4. #4
    Join Date
    Nov 2010
    ? your post is empty...

    My fix to make it a max limit of 50 users per page made the query even a tad slower (as it required yet another 'inner query', but at least it takes less then 2s to generate now with a limit of 50 records per page...

    These queries are so crazily slow im almost thinking it would be 100x faster (literally) in the end to code a PHP solution where I simple send three seperate query to all three tables then have PHP figure it all out. But that kind of defeats the point of SQL.

  5. #5
    Join Date
    Sep 2011
    how you relate your tables?
    users (ID, name)
    fields (ID, title)
    entries (nameid,titleid,value)
    you tables not related... it is does not make sense....
    Of cause if you creating thousands records and transferred them from SQL server to you page it takes time and slow down you page, but if you will get just 50 records from database and display them it will fly...

  6. #6
    Join Date
    Nov 2010
    my tables arent linked with constraints, but here is the schema


    Every row in the table 'fields' is something that every single client can have set in the table 'clentextra'.

    So the table 'clientextra' are entries (where the data is). So assuming the table 'fields' has 10 entries/rows, then each client will have up to 10 entries in table 'clientextra' (I say up to, as its possible some would not be set if they are either optional or a new field was created after the client was added).

    So I want a query where id get a record back for (number of client * number of fields) where if a corresponding entry was found in 'clientextra' it would be displayed, else it would give me nothing for that particular row.

    In retrospect, another way of going about this: Have one query so I know how many items in table 'fields' then a simple query where the client ID and fields ID is found in the 'clientextra'. From that I could write some PHP logic that would know to generate empty/blank textboxes...
    Last edited by ZeratulsDomain; 02-05-2013 at 03:41 PM. Reason: flying monkeys

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14927 seconds
  • Memory Usage 2,890KB
  • Queries Executed 14 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • fetch_postattach_query
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates