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!
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.
Hello
i'm trying to make a table with this query but i cant get it to work. the thing is that it cant select 2 query's at a time but i dont know another way for it..
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
echo $row['username'], "<br/>";
echo $row['n'], "<br/>";
}
Try this:
$active_ids = '1, 3, 4';
$result = $mysqli->query("
(SELECT * FROM users WHERE id IN ({$active_ids}))
UNION
(SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj)
") ;
if (!$rec = mysqli_fetch_array($result)) {
echo ("Sorry, no records found");
}
else {
do {
echo ($rec["username"]);
echo "<br />";
echo ($rec["n"]);
echo "<br />";
}
while ($rec = mysqli_fetch_array($result));
}
$active_ids = '1, 3, 4';
$query = "SELECT users.* from users
LEFT JOIN timetable ON users.id=timetable.dj
WHERE users.id IN ({$active_ids})
UNION
SELECT timetable.dj,timetable.count(*) as n from timetable
RIGHT JOIN users ON timetable.dj=users.id
WHERE timetable.dj IN ({$active_ids}) GROUP BY timetable.dj
";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
echo $row['username'], "<br/>";
echo $row['n'], "<br/>";
}
I don't know it is certainly true but can you try this code ?
I would like to know what the fastest way is to make the following SQL call using PHP. I am using procedural mySQLi.
$dcount = "SELECT max(id) FROM deposits";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(id)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
//second query
$ucount = "SELECT max(int) FROM anothertable";
$result2 = mysqli_query($conn,$ucount);
if (mysqli_num_rows($result2) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(int)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
Is there a way to make the execution faster than like this, maybe to echo the results from both queries after the first if statement?
It just seems rather long to me.
Thanks in advance.
SELECT max(id) as max_id, (SELECT max(int) as max_int FROM anothertable) as max_int
FROM deposits
Not tested, but something like it should work
$dcount = "
SELECT max(id) as max,'deposit' as table_name FROM deposits
UNION
SELECT max(id) as max,'another' as table_name FROM anothertable
";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row["table_name"].":".$row["max"]."\n";
}
} else {
echo '0';
}
SELECT d.max(id) as d_max_id, a.max(int) as a_max_int FROM deposits as d JOIN anothertable as a ON d.id = a.id;
is what you need for multiple tables
$row['d_max_id']
will give you deposits.max(id) now
You have to edit the d.id = a.id accordingly to what you want the two tables to match on
If you cant join try this:
SELECT max(id) as max_id, (SELECT max(int) FROM anothertable) as max_int FROM deposits;
SELECT max(id) as max_id, max(int) as max_int FROM deposits ,anothertable
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>";