I have 3 mysql tables. I want to display leaderboard for team in descending order of d_money for particular day (like day 1, day 2, day 3.)
user(u_id(p),name)
team(t_id(p),u_id(f),t_name,t_money,days_money) and
history(t_id(f),day,d_money).
First i collected all t_id's in $tid_arr. Then for each t_id, i wrote query to get t_name and its days money.(for particular day. Here - day 1). It displays the result. But i want the result sorted (descending order of d_money). But i couldn't find the soultion.
$query = $con->prepare("SELECT t_id FROM team");
$query->execute();
$tid_arr = $query->fetchAll();
echo "<table border='1'>";
foreach($tid_arr as $tid)
{
$que = $con->prepare("SELECT d_money, t_name FROM team, history WHERE history.t_id=$tid['t_id'] AND team.t_id=history.t_id` AND history.day='1'");
$que->execute();
while($info = $que->fetch(PDO::FETCH_NUM))
{
echo "<tr>";
echo "<td>".$info[0]."</td>";
echo "<td>".$info[1]."</td>";
echo "</tr>";
}
}
echo "</table>";
If you want to order by d_money then t_id, you can use the following query. The 1st query is unnecessary and should be removed.
SELECT d_money, t_name FROM team a
LEFT JOIN history b ON a.t_id=b.t_id
WHERE history.day='1'
ORDER BY d_money DESC, t_id DESC
p.s. user table is not used ?
Add a Join and Order By clause to your SQL statement to be
echo "<table border='1'>";
$que = $con->prepare("SELECT T.t_id, H.d_money, T.t_name
FROM team T INNER JOIN history H ON T.t_id=H.t_id
WHERE H.day='1'
ORDER BY d_money DESC");
$que->execute();
while ($info = $que->fetch(PDO::FETCH_NUM)) {
echo "<tr>";
echo "<td>".$info[0]."</td>";
echo "<td>".$info[1]."</td>";
echo "</tr>";
}
echo "</table>";
Related
I trying to create the backend for a booking system and need to show all booked appointments on each date for each location. My appointments table looks like this:
id, booked_date, location_id, customer_id
and I would like to display this in a html table like this:
Date 1
Date 2
Location 1 Name
Number of booked appointments
Number of booked appointments
Location 2 Name
Number of booked appointments
Number of booked appointments
I have a separate table of Locations that has full details like and and address.
I also have a table of event dates (id, start_date)
Im struggling to comprehend what I need to do here!
EDIT:
I just need some help putting everything together into the example table above.
Database tables : appointments, locations, event_dates
I have this query and the function below - SELECT t1.location_id, t1.start_datetime, t2.name, COUNT(*) AS count FROM appointment t1 INNER JOIN location t2 ON t1.location_id = t2.id GROUP BY t1.location_id, t1.start_datetime
function countAppointment() {
require 'config.php';
$sql = "SELECT t1.location_id, t1.start_datetime, t2.name, COUNT(*) AS count
FROM appointment t1
INNER JOIN location t2
ON t1.location_id = t2.id
GROUP BY t1.location_id, t1.start_datetime";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$a1 = $row['start_datetime'];
$a2 = $row['location_id'];
$a3 = $row['name'];
$a4 = $row['count'];
//echo "$a1 <br> $a2 <br> $a3 <br> $a4 <br><br>";
echo "<tr><th scope='row'>$a3</th>";
echo "<td><a href='#'>$a4</a></td>";
echo "<td>9</td><td>27</td><td>14</td></tr>";
}
} else {
return "0";
}
$conn->close();
}
try this code
<?php
$con = new mysqli("localhost","root","","test");
// Check connection
if ($con -> connect_errno) {
echo "Failed to connect to MySQL: " . $con -> connect_error;
exit();
}
$data = array();
$date_list= array();
$sql_date_list ="SELECT start_datetime from appointment GROUP BY start_datetime";
$result_date_list = mysqli_query($con, $sql_date_list);
$sql_loc_list = "SELECT `name` FROM `location` GROUP BY `name`";
$result_loc_list = mysqli_query($con, $sql_loc_list);
$k=0;
while ($row =mysqli_fetch_assoc($result_loc_list)){
$data +=[$row['name']=>array()];
if($k==0){
while($row2 =mysqli_fetch_assoc($result_date_list)){
array_push($date_list,$row2['start_datetime']);
$data[$row['name']]+=[$row2['start_datetime']=>0];
}
$k++;
}else{
foreach($date_list as $date){
$data[$row['name']]+=[$date=>0];
}
}
}
$sql_getdata = "SELECT t2.name as loc_name, t1.start_datetime, COUNT(t1.location_id) AS count FROM appointment t1 JOIN location t2 ON t1.location_id = t2.id GROUP BY t1.location_id,t1.start_datetime";
$result = mysqli_query($con, $sql_getdata);
while($row =mysqli_fetch_assoc($result)){
$data[$row['loc_name']][$row['start_datetime']]=$row['count'];
}
$table="<table border='1'>";
$table.="<tr>";
$table.="<th>location</th>";
foreach ($date_list as $date) {
$table.="<th>".$date."</th>";
}
$table.="</tr>";
foreach ($data as $key=>$date) {
$table.="<tr>";
$table.="<td>".$key."</td>";
foreach($date as $key2=>$count){
$table.="<td>".$count."</td>";
}
$table.="</tr>";
}
$table.="<table>";
echo $table;
?>
output
This is a small section of code i've been working with.
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->here i want percentage</td>";
echo "</tr>";
}
?>
What I would like to do is display the percentage of total/target in the final column of my table. Not sure what the best way to do this is and would be keen to hear some ideas for this.
I did try doing it just as total/target in the echo but thinking it probably needs to be evaluated earlier as a variable or something? Whatever i tried didn't work anyway...
Looked at this but not sure how to implement it in my scenario - MSSQL display percentage calculated from two columns
Try this,
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp, (ifnull(t.total,0)/target)*100 as present FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->present</td>";
echo "</tr>";
}
?>
Here is logic
((cast("A/B" as float)/"C") * 100.00) AS "D%"
all value must be integer and when you are dividing them it must be convert as a float so calculation goes correctly.
Hope it will work for you.
I can't figure out join..
need to select all rows from one table where a column equals something in another table..
like this:
SELECT ALL FROM someTable
WHERE COLUMN someColumn = '123' (IN A DIFFERENT TABLE)
something like that..
and the IDs need to match of course..
Just use an INNER JOIN:
SELECT *
FROM SomeTable S
JOIN SomeOtherTable S2
ON S.SomeKey = S2.SomeKey
WHERE S.SomeColumn = '123'
A Visual Explanation of SQL Joins
I wasn't completely clear of your question, so you may not need the WHERE clause if that represented your JOIN criteria.
A bit error in my question, let me clarify
tblplace
id place
1 London
2 Paris
3 New York
tbltraveller
tr_id tr_name id
1 John 3
2 Jackson 2
3 Susanna 1
4 Jimmy 3
5 Lucy 2
The problem is missing some data for New York:
Paris Jackson Lucy
New York
My code(guess there is an error in the while loop):
<?php
require_once("connMysql1.php");
$qry = "SELECT * FROM tblPlace WHERE tblPlace.id > 1 ORDER BY tblPlace.id";
$result = mysqli_query($db_link, $qry);
$qry1 = "SELECT tblPlace.id, tblPlace.Place, tbltraveller.tr_id,
tbltraveller.tr_name, tbltraveller.id FROM tblPlace INNER JOIN tbltraveller ON
tblPlace.id = tbltraveller.id WHERE tblPlace.id > 1 ORDER BY tbltraveller.id";
$result1 = mysqli_query($db_link, $qry1);
$no_of_row1 = mysqli_num_rows($result1);
$no_of_row = mysqli_num_rows($result);
echo "<table border=1 cellpadding=10>";
if ($no_of_row >0){
while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>";
echo $row['place']."<br>";
echo "</td>"." ";
echo "<td>";
while ($row1 = mysqli_fetch_assoc($result1)){
if($row1['id'] == $row['id']){
echo $row1['tr_name']." ";
}
}
echo "</td>";
"</tr>";
}
}
echo "</table>";
?>
`
I have managed to create a JOIN query for three tables and can successfully echo out the results in a echoed table, here is my code:
<?php
$sql="SELECT a.product_id, a.Options_id, b.product_name, b.product_price, c.Options_name, c.Price_diff
FROM ProductOptions a
JOIN Products b ON a.product_id = b.product_id
JOIN Options c ON a.Options_id = c.Options_id
ORDER BY product_name DESC";
$result = mysql_query($sql);
if (!$result)
{
echo "An error occurred ".mysql_error();
exit;
}
echo "<table border=1>\n<tr><th></th><th bgcolor=\"#DFE8EC\">Name</th><th>Flavors & Size</th><th bgcolor=\"#DFE8EC\">Price</th><th>Price Difference</th><th bgcolor=\"#DFE8EC\"></th></tr>\n";
while ($line = mysql_fetch_array($result)) {
$name = $line["product_name"];
$price = $line["product_price"];
$options=$line["Options_name"];
$difference=$line["Price_diff"];
echo "<tr><td></td><td bgcolor=\"#DFE8EC\">$name</td><td>$options</td> <td bgcolor=\"#DFE8EC\">£$price</td><td>£$difference</td><td bgcolor=\"#DFE8EC\"></td></tr>\n";
}
echo "</table>\n";
?>
My table works but it shows duplicate entries for product_name and I do not know how to remove them.
You have to use a GROUP BY clause in your query like this:
$sql = "SELECT a.product_id, a.Options_id, b.product_name, b.product_price, c.Options_name, c.Price_diff
FROM ProductOptions a
JOIN Products b ON a.product_id = b.product_id
JOIN Options c ON a.Options_id = c.Options_id
GROUP BY product_name
ORDER BY product_name DESC";
I've looked around and I can't seem to find an answer to the following:
All I want to do is run an SQL select statement, to show the user something providing the invoice number (unique ID in this case) isn't showing in another table on the same database, at the moment the code is as follows:
$query = mysql_query("SELECT `ordate`, `invno` FROM `armast09` WHERE cshipno = '$username' AND `ordate` > DATE_SUB(CURDATE(), INTERVAL 4 DAY)") or die(header("location:index.php?e=4"));
$numrows = mysql_num_rows($query);
$date = date('o-n-d');
include 'indexfiles/top.php';
echo "The following deliveries are available for viewing / dispute:";
echo "<br />";
echo "<br />";
echo "<center>";
echo "<table>";
if($numrows != 0) {
while ($info = mysql_fetch_array($query)) {
echo "<tr>";
echo "<form action = \"deliverydispute.php?invno=".$info['invno']."\" method=\"POST\ onsubmit=\"return confirm('Are you sure you are ready to dispute? You can't go back after this.');\">";
echo "<td><b>Invoice Number: </b>".$info['invno']."</td>";
echo "<td><b>Order Date: </b>".$info['ordate']."</td>";
echo "<td>Dispute</td>";
echo "</form>";
echo "</tr>";
}
} else {
echo ("<font color=\"red\" face=\"arial\" size=\"2\"><small>No orders are currently available for viewing.</small></font>");
}
echo "</table>";
You can use the NOT IN predicate:
SELECT ordate, invno
FROM armast09
WHERE cshipno = '$username'
AND ordate > DATE_SUB(CURDATE(), INTERVAL 4 DAY)
AND invno NOT IN(SELECT invno FROM Anothertable WHERE invno IS NOT NULL);
Or LEFT JOIN:
SELECT a.ordate, a.invno
FROM armast09 a
LEFT JOIN anothertable a2 ON a.invno = a2.invno
WHERE a.cshipno = '$username'
AND a.ordate > DATE_SUB(CURDATE(), INTERVAL 4 DAY)
AND a.invno IS NULL;
You can do this easiy with the following:
SELECT X FROM TABLEA LEFT JOIN TABLEB ON TABLEA.ID = TABLEB.ID WHERE TABLEB.ID is null
That should do it!