PHP - Moved: Mysql Left Join Problems
This topic has been moved to MySQL Help.
http://www.phpfreaks.com/forums/index.php?topic=323935.0 Similar TutorialsThis topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308347.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308855.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=352028.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=343994.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=342696.0 Hello all
I have the need to join up two tables and display the output in a table. These tables are a 'Site Details' table and a 'Customer Details' table where the 'cust_id' (PK on the CD table and FK on the SD table) match.
The MySQL statement (within PHP) I have works a treat in as much as the join seems to do the trick and join the two up on the correct row:
<?php if($letter == 'ALL') {$query = mysqli_query($con, SELECT site_details.site_id, site_details.sitename, site_details.sitecode, site_details.postcode,customer_details.cust_number, customer_details.cust_fname FROM site_details LEFT JOIN customer_details ON site_details.cust_id = customer_details.cust_id ORDER BY sitename ASC); } Here is what I am trying to accomplish: I have a students table with a studentID I also have a notes table and a sched table with studentID The sched table is working as planned where if the student is scheduled for more than one time he is displayed twice. However if there are multiple notes per student the student is display for each individual note where I would like only the most current note to be displayed Here is what the output is: 1- 10:30:00 - 10:50:00 student3 Three FST Teacher One Special1 One Writing Ratios 09/10 2- 10:30:00 - 10:50:00 student3 Three FST Teacher One Special1 One Needs to work on fractions and decimals 09/10 3- 13:00:00 - 14:00:00 student3 Three FST Teacher One Special1 One Writing Ratios 09/10 4- 13:00:00 - 14:00:00 student3 Three FST Teacher One Special1 One Needs to work on fractions and decimals 09/10 As you can see I have two notes and two schedule times for this student. what I want it to display is only line 1 & 3. which is the newest note in the system. Below is my code that I am using. Any help would be greatly appreciated. Code: [Select] <?php $result = mysql_query("SELECT * FROM students LEFT JOIN teachers ON students.teacherId = teachers.teacherId LEFT JOIN course ON students.courseId = course.courseId LEFT JOIN specialEd ON students.specialId = specialEd.specialId LEFT JOIN sched ON students.studentId = sched.studentId ORDER BY start "); echo "<table>"; while ($row = mysql_fetch_array($result)){ $id = "?id=" . $row['studentId']; echo "<tr>"; echo "<td>" . $row['start'] . " - " . $row['stop'] . "</td>"; echo "<td>" . "<a href='student.php$id'>" . $row['fName'] . " " . $row['lName'] . "</td>"; echo "<td>" . $row['courseName'] . "</td>"; echo "<td>" . $row['teachers_fName'] . " " . $row['teachers_lName'] . "</td>"; echo "<td>" . $row['special_fName'] . " " . $row['special_lName'] . "</td>"; echo "<td>" . $row['note'] . "</td>"; echo "<td>" . date("m/d", strtotime($row['started'])) . "</td>"; echo "</tr>"; } echo "</table>"; ?> This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=351625.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=348196.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321929.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321443.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306058.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=313679.0 I am using the following sql code to display a stream of statuses, I have the username/profile picture/status/likes working but I am now stuck on comments.
SELECT s.*, u.*, c.*, COUNT(l.likes_location_id) AS likeCount FROM stream AS s LEFT JOIN users as u ON (u.users_username = s.stream_username) LEFT JOIN comments AS c ON ( c.comments_location_id = s.stream_id ) LEFT JOIN likes AS l ON ( l.likes_location_id = s.stream_id ) GROUP BY s.stream_id ORDER BY s.stream_id DESC LIMIT 50If a status has multiple comments the results only shows the first comment that was made. How can I get it to include all of them? Also, how would I include this in the PHP loop, without having another loop? Thanks, Edited by slj90, 06 January 2015 - 08:56 PM. Alright this is going to be a doozy: I have 3 tables: Collectibles, CollectiblesMethods and CollectionsCollectibles Collectibles contains: ID, Photo, Collectible, Value, Resale, Rarity CollectiblesMethods contains: ID, Collectible, Method CollectionsCollectibles contains: ID, Collectible, Collection, Amount Needless to say, this is a many to many relationship model. (many methods to one collectible, many collectibles to one collection). The page collectibles.php is contained inside collspec.php collspec.php shows information relating to a specific collection called by GET. collectibles.php shows all collectibles belonging to the same specfic collection in collpec.php, thus why it is contained inside it. I want collectibles.php to output the following: Photo, Collectible, Amount, Resale, Rarity, Method I already have collectibles.php outputting: Photo, Collectible, Amount, Resale, Rarity Method is proving to be difficult. I can either get it to show the word None for every collectible with this code: <table width="99%"> <tr> <th colspan="6"><h2>Collectibles</h2></th> </tr> <tr> <td>Photo</td> <td>Name</td> <td># Needed</td> <td>Sells For</td> <td>Rarity</td> <td>Obtained</td> </tr> <?php $sql2 = "SELECT * FROM CollectionsCollectibles LEFT JOIN Collectibles ON CollectionsCollectibles.Collectible = Collectibles.Collectible WHERE Collection = '" . mysql_real_escape_string($item) . "'"; $collectible = mysql_query($sql2); if (trim($row['Collectible']) != '') while($row2 = mysql_fetch_array($collectible)){ ?> <tr> <td width="14%" valign="top"><img src="<?php echo "$img{$row2['Photo']}"; ?>"></td> <td><?php echo $row2['Collectible']; ?></td> <td><?php echo $row2['Amount']; ?></td> <td><?php echo $row2['Sell']; ?></td> <td><?php echo $row2['Rarity']; ?></td> <td> <?php $sql3 = "SELECT CollectiblesMethods.Method FROM CollectiblesMethods WHERE Collectible = '" . $row2['Collectible'] . "'"; $obtain = mysql_query($sql3); if (trim($row['Method']) != '') while($row3 = mysql_fetch_array($obtain)){ ?> <tr> <td><?php echo $row3['Method']; ?></td> </tr> <?php } else echo 'None'; ?> </td> <?php } else echo 'None'; ?> </tr> </table> or I can get it to show the Methods (of which certain collectibles have more than one method) using this code but with a catch; it repeats the photo, name, value and resale if there is more than one method for a given collectible: <table width="99%"> <tr> <th colspan="6"><h2>Collectibles</h2></th> </tr> <tr> <td>Photo</td> <td>Name</td> <td># Needed</td> <td>Sells For</td> <td>Rarity</td> <td>Obtained</td> </tr> <?php $sql2 = "SELECT * FROM CollectionsCollectibles LEFT JOIN Collectibles ON CollectionsCollectibles.Collectible = Collectibles.Collectible LEFT JOIN CollectiblesMethods ON Collectibles.Collectible = CollectiblesMethods.Collectible WHERE Collection = '" . mysql_real_escape_string($item) . "'"; $collectible = mysql_query($sql2); if (trim($row['Collectible']) != '') while($row2 = mysql_fetch_array($collectible)){ ?> <tr> <td width="14%" valign="top"><img src="<?php echo "$img{$row2['Photo']}"; ?>"></td> <td><?php echo $row2['Collectible']; ?></td> <td><?php echo $row2['Amount']; ?></td> <td><?php echo $row2['Sell']; ?></td> <td><?php echo $row2['Rarity']; ?></td> <td><?php echo $row2['Method']; ?></td> <?php } else echo 'None'; ?> </tr> </table> So Let's say I have a collection called Toys. This is how it outputs using my above codes: 1st Code example: __________________________________________ |Photo|Name |Amount|Sells For |Rarity |Obtained| |Pic1 |Marble| x5 | 20 cents | Common | None | |Pic2 |Train | x1 | 5 $ | Rare | None | 2nd Code example: __________________________________________ |Photo|Name |Amount|Sells For |Rarity |Obtained| |Pic1 |Marble| x5 | 20 cents | Common | Barter | |Pic1 |Marble| x5 | 20 cents | Common | Trade | |Pic2 |Train | x1 | 5 $ | Rare | Gift | How it should output: __________________________________________ |Photo|Name |Amount|Sells For |Rarity |Obtained| |Pic1 |Marble| x5 | 20 cents | Common | Barter | | | | | | | Trade | |Pic2 |Train | x1 | 5 $ | Rare | Gift | As you can see, I want it to output all methods obtained in the same box, increasing the rowspan as necessary. I don't want it to output None (since this is not true) as in the first example. I don't want it to repeat the same information simply to add a line for the next method as in the second example. Can someone help me figure out what I'm doing wrong? $compQ = "SELECT companies.companyid, companies.companyname, companies.companylogo, companies.companyoccupation, companies.industry, eQuestions.capitalrequested FROM companies LEFT JOIN eQuestions ON companies.companyid = eQuestions.companyid"; This is not displaying data correctly. I'm assuming eQuestions.capitalrequested is not in the correct spot? I need to set up pagination. I've seen a tutorial online, and in a book I have. But they're both for basic queries. I've got a LEFT JOIN. The basic script doesn't seem to work. Is there a different way it has to be done for joins? I am trying to write some code to display the visits to my site each week in batches of 13 weeks (1 quarter).
I am getting the Week No and the number of hits, but the actual date, which is in a separate table is not being displayed.
I have tried LEFT JOIN and INNER Join and even RIGHT JOIN (this only displays the Date).
I can't see where I am going wrong!
Here is the code:
<? include('connect_visits.php'); doDB7(); $limitStart = $_POST['QUARTER'] - 13; $Visit_data="SELECT Visits.ID, Visits.WVisits, Visits.WCom, WeekNo.WNo, WeekNo.WCom FROM Visits LEFT JOIN WeekNo ON Visits.ID = WeekNo.WCom ORDER BY Visits.ID LIMIT {$limitStart}, 13"; $Visit_data_res = mysqli_query($mysqli, $Visit_data) or die(mysqli_error($mysqli)); $display_block = " <table width=\"20%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"white\" > <tr> <th>Week No</th> <th>Week Commencing</th> <th>Visits</th> </tr>"; while ($C_info = mysqli_fetch_array($Visit_data_res)) { $Cid = $C_info['ID']; $Visits = ($C_info['WVisits']); $WeekNo = ($C_info['WCom']); //add to display $display_block .= " <tr> <td width=\"1%\" valign=\"top\">".$Cid."<br/></td> <td width=\"8%\" valign=\"top\">".$WeekNo."<br/></td> <td width=\"5%\" valign=\"top\">".$Visits."<br/></td> </td></tr>"; } mysqli_free_result($Visit_data_res); ?>Where am I going wrong? Edited by rocky48, 29 December 2014 - 12:53 PM. Hi there i have 3 tables joined together and am outputting the results ok. Heres what the results look like: Fleet Name: Home One Detected: No Ship Name: Slave 1 Bobafett Hull: 3 / 2 Fleet Name: Home Two Detected: Yes Ship Name: Imperial Star Destroyer Hull: 2 / 4 Fleet Name: Home Two Detected: Yes Ship Name: Executer Hull: 10 / 10 The 3 tables join together by the following: Planet = parsed url parameter Fleets = fleets on the current planet Ships = current ships in the fleets Its all working fine except for one thing, the Fleet Name is repeated for each occurance of a ship. I want to make it so that Fleet Name only occurs once for each fleet so it would look something like: Fleet Name: Home One Detected: No Ship Name: Slave 1 Bobafett Hull: 3 / 2 Fleet Name: Home Two Ship Name: Imperial Star Destroyer Detected: Yes Hull: 2 / 4 Ship Name: Executer Detected: Yes Hull: 10 / 10 Is this possible??? Heres the code in full: Code: [Select] <?php require_once('Connections/swb.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_Planet = "-1"; if (isset($_GET['recordID'])) { $colname_Planet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']); } mysql_select_db($database_swb, $swb); $query_Planet = sprintf("SELECT * FROM planet WHERE PlanetName = %s", GetSQLValueString($colname_Planet, "text")); $Planet = mysql_query($query_Planet, $swb) or die(mysql_error()); $row_Planet = mysql_fetch_assoc($Planet); $totalRows_Planet = mysql_num_rows($Planet); $colname_Fleet = "-1"; if (isset($_GET['recordID'])) { $colname_Fleet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']); } mysql_select_db($database_swb, $swb); $query_Fleet = sprintf("SELECT p.PlanetName, p.PlayerName, f.FleetName, f.Detected, s.ShipName, s.HealthA, s.HealthB FROM Planet p LEFT JOIN Fleet f ON (p.PlanetName = f.PlanetName) LEFT JOIN Ships s ON (f.FleetName = s.FleetName) WHERE p.PlanetName = '$colname_Planet'"); $Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error()); $row_Fleet = mysql_fetch_assoc($Fleet); $totalRows_Fleet = mysql_num_rows($Fleet); ?> <head> <title>Untitled Document</title> <style type="text/css"> <!-- body { background-color: #FFFFFF; } --> </style></head> <body> <table width="60%" border="0"> <tr> <td width="10%">Planet:</td> <td width="14%"><?php echo $row_Planet['PlanetName']; ?></td> <td width="7%" rowspan="8"> </td> <td colspan="2" rowspan="8"> <?php while ($row_Fleet = mysql_fetch_assoc($Fleet)) { echo '<table width="400" border="0"><tr><td width="200">'; echo 'Fleet Name: '; echo'</td><td>'; echo $row_Fleet['FleetName']; echo'</td></tr><tr><td>'; echo 'Detected: '; echo'</td><td>'; if ($row_Fleet['Detected'] == '1'){ echo 'Yes';} else echo 'No'; echo'</td></tr><tr><td>'; echo 'Ship Name: '; echo'</td>'; echo'<td>'; echo $row_Fleet['ShipName']; echo'</td></tr><tr><td>'; echo 'Hull: '; echo'</td><td>'; echo $row_Fleet['HealthA']; echo ' / '; echo $row_Fleet['HealthB']; echo '</td></tr><tr><td></td></tr>'; echo'</table>'; } ?> <tr> <td>Faction:</td> <td><?php echo $row_Planet['PlayerName']; ?></td> </tr> <tr> <td>Infastructure</td> <td> </td> </tr> <tr> <td>Class 1: </td> <td><?php echo $row_Planet['Class1']; ?></td> <tr> <td>Class 2: </td> <td><?php echo $row_Planet['Class2']; ?></td> </tr> <tr> <td>Class 3: </td> <td><?php echo $row_Planet['Class3']; ?></td> </tr> <tr> <td>Class 4: </td> <td><?php echo $row_Planet['Class4']; ?></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> <a href="index.php target="main2.php"">Back</a></td> <td> </td> <td> </td> <td width="43%"> </td> <td width="26%"> </td> </tr> </table> </body> </html> <?php mysql_free_result($Planet); mysql_free_result($Fleet); ?> Thanks I need to echo the "reward.id" Code: [Select] $sql = "select reward.id, reward.location, reward.industry, reward.title, reward.stamp, industry.id, industry.label, industry.location FROM reward LEFT JOIN industry ON industry.id = reward.industry where (industry.label = '".$_GET['ind']."' and industry.location = '".$_GET['loc']."')"; $result = mysql_query($sql) or die(mysql_error()); while($info = mysql_fetch_assoc($result)){ echo $info['HOW TO I PRINT reward.id']; }free($result ); Any thoughts? |