Avian Gamers Network http://avian-gamers.net/forums/ |
|
PHP Help http://avian-gamers.net/forums/viewtopic.php?f=1&t=12593 |
Page 1 of 1 |
Author: | iJasonT [ Tue Aug 24, 2004 4:18 pm ] |
Post subject: | PHP Help |
I have two tables I need to search through and see if an id exists in one but not the other. npc.npcid and npc_2_loot.npcid I need to be sure that all npc.npcid appears at least once in npc_2_loot.npcid. If It does not appear once I need it to list out the ones that did not show up in a search. |
Author: | iJasonT [ Tue Aug 24, 2004 4:28 pm ] |
Post subject: | |
/blows dust off the not often used LEFT JOIN SQL command ![]() Code: $query_rs_npc = "SELECT npc.npcid
FROM npc LEFT JOIN npc_2_loot ON npc_2_loot.npcid = npc.npcid WHERE npc_2_loot.npcid IS NULL ORDER BY npc.npcid ASC"; |
Author: | kebernet [ Tue Aug 24, 2004 4:30 pm ] |
Post subject: | |
SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN ( SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot ); |
Author: | Obo [ Tue Aug 24, 2004 4:32 pm ] |
Post subject: | |
Select all the ids from npc and then do a do while loop. Inside the loop make a second database query where you search for the specific id in npc_2_loot (the loop will go through each id returned from the first query). If you don't return a result print a message. or a left join ![]() |
Author: | kebernet [ Tue Aug 24, 2004 4:33 pm ] |
Post subject: | |
iJasonT wrote: /blows dust off the not often used LEFT JOIN SQL command
![]() Code: $query_rs_npc = "SELECT npc.npcid FROM npc LEFT JOIN npc_2_loot ON npc_2_loot.npcid = npc.npcid WHERE npc_2_loot.npcid IS NULL ORDER BY npc.npcid ASC"; I dont think that would ever actually give you results. That would assume that npc.npcid is null on the npc table too. If you drop the WHERE clause completely, though, and do: SELECT npc.npcid, npc_2_loot.npcid AS loot_id FROM npc LEFT JOIN npc_2_loot ON npc_2_loot.npcid = npc.npcid ORDER BY npc.npcid ASC You could cycle through the results and look for npcid == somethign and loot_id == null. |
Author: | iJasonT [ Tue Aug 24, 2004 4:35 pm ] |
Post subject: | |
hehe three posts three answers that all work. Man you gotta love PHP. OK here is an easy one. I don't use Functions much so Im a bit rusty here. I get 6 results but only 5 are listed via my loop. I had this problem before where the first result is skipped, what do I do to get all 6 to list out? |
Author: | Arindel [ Tue Aug 24, 2004 4:35 pm ] |
Post subject: | |
kebernet wrote: SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN ( SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot );
SELECT npc.npcid FROM npc WHERE NOT EXISTS( SELECT npc_2_loot.npcid FROM npc_2_loot WHERE npc.npcid = npc_2_loot.npcid);[/ |
Author: | iJasonT [ Tue Aug 24, 2004 4:44 pm ] |
Post subject: | |
Keb. It worked for me fine. I'm loading in a the other examples here to see if I get differing results. |
Author: | kebernet [ Tue Aug 24, 2004 4:44 pm ] |
Post subject: | |
iJasonT wrote: hehe three posts three answers that all work. Man you gotta love PHP.
OK here is an easy one. I don't use Functions much so Im a bit rusty here. I get 6 results but only 5 are listed via my loop. I had this problem before where the first result is skipped, what do I do to get all 6 to list out? Im not sure how PHP does SQL resultsets. I think you just get an indexed array of the results. The big thing is, you have to do your null check as the loop control. Something like: while( $row = fetch_array( $resultset ) ){ // handle loop. } If you are wanting to call a function I *think* you can just do a null check on the results set: if( $resultset ){ my_function( $resultset ); } |
Author: | kebernet [ Tue Aug 24, 2004 4:46 pm ] |
Post subject: | |
Arindel wrote: kebernet wrote: SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN ( SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot ); SELECT npc.npcid FROM npc WHERE NOT EXISTS( SELECT npc_2_loot.npcid FROM npc_2_loot WHERE npc.npcid = npc_2_loot.npcid);[/ That would work too, but if I were at the office ![]() ![]() |
Author: | iJasonT [ Tue Aug 24, 2004 5:02 pm ] |
Post subject: | |
I am having trouble with this one and cant seem to find my syntax error. Code: $query_rs_npc = "SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN (SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot)"; $row_npc = mysql_query($query_rs_npc) or die(mysql_error()); $row_rs_npc = mysql_fetch_assoc($row_npc); $rs_npc = mysql_num_rows($row_npc); echo $rs_npc; Code: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN (SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot)' at line
|
Author: | kebernet [ Tue Aug 24, 2004 5:04 pm ] |
Post subject: | |
Aaah. Yeah. Most version of MySQL don't support nested selects ![]() ![]() |
Author: | Arindel [ Tue Aug 24, 2004 5:04 pm ] |
Post subject: | |
That depends if the DB in question does a full select statement run for each row you are checking. ![]() The Exists function usually forces it to a signle point in the where clause. But, I usually like your approach as it reads a bit easier. Just offering other possibilities ![]() and I don't see any Syntax error unless it is looking for a end/terminator for the SQL where (like a semicolon). Even then I don't think it would be around the IN statement |
Author: | iJasonT [ Tue Aug 24, 2004 5:06 pm ] |
Post subject: | |
yeah I have the WHERE npc.npcid NOT IN (was an error in the above post) but it's still no joy. |
Author: | iJasonT [ Tue Aug 24, 2004 5:15 pm ] |
Post subject: | |
ahh nesting is the problem. Obbo's and mine seem to return the same results so I think all is good. Still having a wierd issue where the first result is not being listed. Very odd. |
Author: | iJasonT [ Fri Aug 27, 2004 2:44 am ] |
Post subject: | |
ok I need more help. I am trying to organize my data into a table. I have 2 db tables I am querying. groupnames and grouploot basically I need it to have 4 columns groupnames over grouploot. There will probably be multiple rows as I have 10-12 grouptitles so far and will probably be adding more. If you know of a tutorial that can walk me through setting this I would appreciate it. |
Author: | iJasonT [ Fri Aug 27, 2004 2:54 pm ] |
Post subject: | |
I had it close when I posted here first I just couldn't get my array right. Im pretty new to Arrays so it took me a while but I came up with this. Works great. Code: $sql = "SELECT s.grouptitle, l.groupname
FROM sgroup s INNER JOIN loot_drop l ON s.sgid = l.sgid"; $res = mysql_query($sql) or die(mysql_error()); # # store names in array for each title # it makes the table columns code a lot simpler # $array = array(); while (list($t, $n) = mysql_fetch_row($res)) { $array[$t][] = $n; } # # now output the array into a 4 column table # define ("NUMCOLS",4); $count = 0; echo "<table border='0' cellpadding='4' cellspacing='2' width='500'>\n"; foreach ($array as $title => $names) { # new row every NUMCOLS cells if ($count % NUMCOLS == 0) echo "<TR valign='top'>\n"; echo " <TD width='25%'>\n"; echo " <div style='border: solid 1pt silver'>$title</div>\n"; # now names for the title foreach ($names as $n) { echo " $n<br/>\n"; } echo " <br/>\n </TD>\n"; $count++; # end row if we've just done NUMCOLs cells if ($count % NUMCOLS == 0) echo "</TR>\n"; } if ($count % NUMCOLS != 0) { # end row if not already ended while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; } echo "</table>\n"; |
Page 1 of 1 | All times are UTC - 5 hours [ DST ] |
Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |