InterDevelop Posted May 19, 2006 Share Posted May 19, 2006 I have query that gives me the perfect set of results I'm looking for. It's narrowed things down from a location table and a services provided table. I use the results (i.e. Member IDs) to pull contact info out of the membership table. However, I need to sort by a ranking number of 1 or more services provided. I'm stumped on how to do an ORDER BY from a field in another table. Has anyone worked with something like this?I've tried the following, but it doesn't work:[code] $sql = "SELECT * FROM Members WHERE MemberID = 552 || MemberID = 562 ORDER BY ServiceProvided.Ranking";[/code]My code is amaturish at best, but trying to get some input that would help me become a better MySQL/PHP developer. :blink: Thanks,Jeff [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/ Share on other sites More sharing options...
char0n Posted May 19, 2006 Share Posted May 19, 2006 well, i am not sure if i understood it correctly but here is my solution.I just guess u have 2 related tables and u want to sort values fromone table according to the value from anotherSELECT t1.ID, t1.text FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.ID=t2.ID WHERE t1.ID='1' ORDER BY t2.Rank ASC LIMIT 0, 30Hope it little bit helped u Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-37221 Share on other sites More sharing options...
fenway Posted May 20, 2006 Share Posted May 20, 2006 [!--quoteo(post=375286:date=May 19 2006, 01:29 PM:name=Jeff Baker)--][div class=\'quotetop\']QUOTE(Jeff Baker @ May 19 2006, 01:29 PM) [snapback]375286[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have query that gives me the perfect set of results I'm looking for. It's narrowed things down from a location table and a services provided table. I use the results (i.e. Member IDs) to pull contact info out of the membership table. However, I need to sort by a ranking number of 1 or more services provided. I'm stumped on how to do an ORDER BY from a field in another table. Has anyone worked with something like this?I've tried the following, but it doesn't work:[code] $sql = "SELECT * FROM Members WHERE MemberID = 552 || MemberID = 562 ORDER BY ServiceProvided.Ranking";[/code]My code is amaturish at best, but trying to get some input that would help me become a better MySQL/PHP developer. :blink: Thanks,Jeff [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /][/quote]How are these tables linked? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-37441 Share on other sites More sharing options...
InterDevelop Posted May 24, 2006 Author Share Posted May 24, 2006 [!--quoteo(post=375516:date=May 20 2006, 11:36 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 20 2006, 11:36 AM) [snapback]375516[/snapback][/div][div class=\'quotemain\'][!--quotec--]How are these tables linked?[/quote]Linked by MemberID in both tables. I think my problem is that I have several ServiceIDs & several MemberIDs that I'm trying to process.I call the [b]Services[/b] table to match a search query of up to 72 items selected. This give me the list of MemberID to pull from the [b]Members[/b] table. However, in the [b]Services[/b] table, I have a seperate field to do a rank (ORDER BY). The ranking is not in the [b]Members[/b] table, so I need to get the member details from the [b]Members[/b] table but ORDER BY the "ranking" field in the [b]Service[/b] table. Hopefully this clarifies it. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-38544 Share on other sites More sharing options...
fenway Posted May 24, 2006 Share Posted May 24, 2006 Try the following (UNTESTED):[code]SELECT m.* FROM Members AS m LEFT JOIN Services AS s ON ( s.MemberID = m.MemberID )WHERE m.MemberID IN ( '552', '562' )ORDER BY s.Ranking[/code]Technically, you're not supposed to be allowed to use a column in a order by that doesn't appear in the column list, but MySQL lets it slide. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-38609 Share on other sites More sharing options...
InterDevelop Posted July 10, 2006 Author Share Posted July 10, 2006 I think I'm almost there..... sort of...My final sql statement:[code]$sql = "SELECT DISTINCT mem.MemberID, mem.FirstName, mem.MiddleName, mem.LastName, mem.CompanyName, mem.MemberTypeID, mem.MemberLevel, mem.Addr1, mem.Addr2, mem.City, mem.State, mem.Zip, mem.Work, mem.Fax, mem.Email FROM ASIDMembers AS mem LEFT JOIN ResServicesProvided AS res ON (res.MemberID=mem.MemberID) WHERE mem.MemberID IN ($members_by_location3) ORDER BY res.ResRanked";[/code][list][*]$members_by_location3 is an array of members passed from a location table earlier on in the search... (1, 201, 144, 509). This limits the results to the correct people, but the ORDER BY element does not order proplery. [*]The res.ResRanked field is a numeric value that theoretically should sort by numbers 1-50.[/list]I do get results, but the ordering still doesn't quite sort it by Rank. is there another way to do this...? ::) Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-55797 Share on other sites More sharing options...
fenway Posted July 11, 2006 Share Posted July 11, 2006 I'm not sure how that's possible... you should return res.ResRanked in your column list, and see what values you get back. Also, be careful with DISTINCT, especially with so many fields -- it can give you strange results. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56056 Share on other sites More sharing options...
InterDevelop Posted July 11, 2006 Author Share Posted July 11, 2006 hmmm. I chose to use DISTINCT because if I don't, the results repeat 44 times from the above the following query:[code]SELECT DISTINCT mem.MemberID, mem.FirstName, mem.MiddleName, mem.LastName, mem.CompanyName, mem.MemberTypeID, mem.MemberLevel, mem.Addr1, mem.Addr2, mem.City, mem.State, mem.Zip, mem.Work, mem.Fax, mem.Email, res.MemberID, res.ResRanked FROM ASIDMembers AS mem LEFT JOIN ResServicesProvided AS res ON (res.MemberID=mem.MemberID) WHERE mem.MemberID IN ($members_by_location3) ORDER BY res.ResRanked, mem.MemberTypeID[/code]I've included the res.MemberID and res.ResRanked in my column list. It seams to pull a better set of results. There still some weird things happening. :o The first issue is that this returns the same results 44 times, even though the list of MemberIDs in the WHERE memMemberID IN ($members_by_location3) statement only is in there once. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56423 Share on other sites More sharing options...
fenway Posted July 12, 2006 Share Posted July 12, 2006 Hard to say without the data in hand; you're probably missing a GROUP BY clause somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56581 Share on other sites More sharing options...
InterDevelop Posted July 12, 2006 Author Share Posted July 12, 2006 o.k.... by using a GROUP BY on the mem.MemberID field, I only get one member returning with the result set... the problem now is that the ORDER BY res.ResRanked field isn't ordering at all. The res.ResRanked field is a TINYINT. Is there an issue sorting by this type of field?... should I change the field type to a text (VARCHAR or TEXT) field type? Should I use letters instead of numbers for these fields if I want to sort by them? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56779 Share on other sites More sharing options...
fenway Posted July 12, 2006 Share Posted July 12, 2006 Really? Can you output the result of your query, including the resRanked column? It doesn't make any sense. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56796 Share on other sites More sharing options...
InterDevelop Posted July 12, 2006 Author Share Posted July 12, 2006 This is the full query that is passed based on selection criteria:[code]SELECT mem.MemberID, mem.FirstName, mem.MiddleName, mem.LastName, mem.CompanyName, mem.MemberTypeID, mem.MemberLevel, mem.Addr1, mem.Addr2, mem.City, mem.State, mem.Zip, mem.Work, mem.Fax, mem.Email, res.MemberID, res.ResRanked FROM ASIDMembers AS mem LEFT JOIN ResServicesProvided AS res ON (res.MemberID=mem.MemberID) WHERE mem.MemberID IN (48, 64, 66, 84, 100, 169, 173, 181, 277, 287, 545, 546, 562, 579, 592, 599, 600, 616, 642, 663, 667, 675, 698, 743) GROUP BY mem.MemberID ORDER BY res.ResRanked ASC, mem.MemberTypeID ASC[/code]The res.ResRanked column (res.MemberID = res.ResRanked) results are:48=164=184=1100=1181=1545=1579=1592=1600=1663=1667=1675=1698=166=2169=2173=2277=2287=2616=2642=2743=2546=3562=4599=4So all the res.MemberIDs that = 1 (i.e. res.ResRanked = 1) should show up first and then 2, then 3, then 4... Hoever, the ordering of the results is as followis: 675, 698, 546, 579, 277, 545, 592, 667, 599, 562, 181, 287, 100, 600, 64, 84, 48, 642, 616, 663, 169, 66, 173, 743So... I'm not sure why it is not ordering by res.ResRanked...?? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56821 Share on other sites More sharing options...
fenway Posted July 12, 2006 Share Posted July 12, 2006 I'm confused... what does the first set of results represent, versus the 2nd set? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56840 Share on other sites More sharing options...
InterDevelop Posted July 12, 2006 Author Share Posted July 12, 2006 The first set of results --- [code]WHERE mem.MemberID IN (48, 64, 66, 84, 100, 169, 173, 181, 277, 287, 545, 546, 562, 579, 592, 599, 600, 616, 642, 663, 667, 675, 698, 743) [/code]are a list of MemberIds that match a set of criteria from from a locations (i.e. city code match) table and a services provided table. I use these IDs to retreive the members data out of the membership table (ASIDMembers). Part of the process is displaying the order by how the member ranked a certain service in the services table (ResServicesProvided). To simplify (hopefully)... [list][*]I have a set of results (1 or more member IDs).[*]I retreive the details from the members table. [*]I need to sort these results by a ranked number (possibly values of 1-30 - res.ResRanked) from the services provided table.[/list]The IDs are correct, but the ranking of these IDs is where I get stuck. Hopefully this clarifies a little. ??? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56864 Share on other sites More sharing options...
fenway Posted July 12, 2006 Share Posted July 12, 2006 I've obviously missed something.. your resRanked varies from 1-4? Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56881 Share on other sites More sharing options...
InterDevelop Posted July 12, 2006 Author Share Posted July 12, 2006 :-\ Those particular ranking were specific to that set of IDs. There are other member IDs in the system that may have a ranking as high as 30. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56886 Share on other sites More sharing options...
fenway Posted July 12, 2006 Share Posted July 12, 2006 Still, I'm not sure why your ORDER BY wouldn't catch... resRanking, memberID should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56945 Share on other sites More sharing options...
InterDevelop Posted July 12, 2006 Author Share Posted July 12, 2006 ??? I'm not sure either.... The sorting works for the [code]ORDER BY MemberTypeID ASC[/code] segment, but the other part just isn't catching... I'm going to take a further look at the actual data in the tables and see if I'm still missing something. Thank you for taking some time with me on this. If I find out anything new, I'll post back to here.Thank you - Jeff Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-56951 Share on other sites More sharing options...
SQL_F1 Posted July 13, 2006 Share Posted July 13, 2006 Hi JeffYou're in luck - I'm using this example in a SQL class I'm preparing so I have some time to devote to the problemThere are 2 problems:1) We have been using a JOIN when we should not have JOINed or JOINed in another way. That resulted in duplicates which we then suppressed in a random way because of problem #22) There is a hole in the business logic of the query.#2 first because #1 cannot be solved without knowing what we actually need.You have a Members table, probably with unique MemberID's/1 row per member in a 1 to 0, 1 or many relationwhip with a Services table joined on MemberID.I presume that most Members have ranked 1 or more Services and some have not ranked any (because you used a left join)So you have Members with multiple rankings, one for each Service. [b]Which service ranking do you want?[/b]problem #1Provide the actual result set from your query - I can't tell in what order it [u][b]is[/b][/u] appearing in.There's no way it will not appear in res.ResRanked ASC, mem.MemberTypeID ASC order in the full query you set out (with the ORDER BY res.ResRanked ASC, mem.MemberTypeID ASC ) - tinyints for values 1 - 30 are just fine - varchar will be even worse (1, 10, 11 ... 2, 21, 22..)The problem with the JOIN and GROUP BY/DISTINCT is that there is no specifying which ranking you want GROUP BY should only be used with aggregate functions (MIN, MAX ) because they specifiy which values you want for each value grouped - if they can't the GROUP BY is inappropriate.The other problem with the query is that it may be the wrong way around.Members are being identified by location and Services provided, then those members are listed with a ranking from the services provided table.The following seems to be what should is required[code]SELECT res.Service, res.ResRanked, mem.MemberID, mem.FirstName, mem.MiddleName, mem.LastName, mem.CompanyName, mem.MemberTypeID, mem.MemberLevel, mem.Addr1, mem.Addr2, mem.City, mem.State, mem.Zip, mem.Work, mem.Fax, mem.Email FROM ResServicesProvided AS res INNER JOIN ASIDMembers AS mem ON (res.MemberID=mem.MemberID) WHERE mem.MemberID IN ($members_by_location3) ORDER BY res.ResRanked;[/code]If this is not what is intended (see problem #2) then there must be some criteria missing from the WHERE clause specifying which single service you want to the the ranking for.I look forward to your resonse.Brian Quote Link to comment https://forums.phpfreaks.com/topic/10022-sorting-by-another-table/#findComment-57128 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.