I am trying to change the date format before it is displayed with SQL query however the date format is being completely ignored.
my code is
$query = "SELECT * , DATE_FORMAT(formatted, '%d/%m/%Y') from movies;";
then further down this is my table
echo "<table>"
echo "<table border='2'>"
echo "<tr>
<th>id</th>
<th>title</th>
<th>date</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['formatted'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
this query is working, however the date format is being ignored and just displaying the date in yyyy-mm-dd I want it in DD-MM-YY.
thanks
Use an alias to name your calculated column
SELECT * , DATE_FORMAT(datetime, '%d/%m/%Y') AS formatted_date
from movies
Use a different name than the existing column to differ between the two. Then use
echo "<td>" . $row['formatted_date'] . "</td>";
to get the formatted one.
You need to mention the alias for the formatted datetime column other wise formatted value will not be called in your code
SELECT * ,
DATE_FORMAT(`datetime`, '%d/%m/%Y') `datetime`
from movies
Related
I have 3 sql tables and I want to make 1 table in html
The tables are:
Norm (norm_id, name, description, cluster_cluster_id, orden_orden_id)
Cluster (cluster_id, cluster_name)
Orden (orden_id, orden_name)
The table must contain:
norm_id - norm_name - norm - description - cluster_name - orden_name
I think i need to do this with a left outer join?
And how to show it in a table?
Right now i have
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['norm_id'] . "</td>";
echo "<td>" . $row['norm_name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['cluster_name'] . "</td>";
echo "<td>" . $row['orden_name'] . "</td>";
echo "</tr>";
}
You can make left outer join like, SELECT column-names
FROM table-name1 LEFT JOIN table-name2
ON column-name1 = column-name2
WHERE condition
Here is the table structure.
tbl_staff
firstname varchar(50)
midname varchar(50)
lastname varchar(50)
...
tbl_staff2
first_day_of_service date
...
Here is the scenario.
For the first 10 years of stay of the staff in organization, he/she will be given a loyalty award and then every 5 years after he/she receives the first 10 years.
My initial code will just get the name of staff and the date of first day of service.
public function actionGet_loyalty() {
$query = (new \yii\db\Query())
->select(['firstname', 'midname', 'lastname', 'first_day_service'])
->from('tbl_staff')
->limit(100)
->offset(0)
->orderBy('first_day_service')
->leftJoin('tbl_staff2', 'tbl_staff2.staff_id = tbl_staff.id');
$command = $query->createCommand();
$data = $command->queryAll();
$string = "<table class='table table-striped'><tr><th>No.</th><th>Name</th><th>Date to Receive Loyalty Award</th></tr>";
$i = 1;
foreach ($data as $row) {
$firstname = $row['firstname'];
$midname = $row['midname'];
$lastname = $row['lastname'];
//$string.=$row['first_day_service'];
$string.="<tr>"
. "<td>$i</td>"
. "<td>" . $firstname . " " . $midname[0] . ". " . $lastname . "</td>"
. "<td>" . $row['first_day_service'] . "</td>"
. "</tr>";
$i++;
}
$string.="</table>";
return $string;
}
How to display all the name of staff and the date he/she will receive the award in increasing order.
Or any mysql query that will sort the name according the date?
If you want to do it via SQL query, in MySQL you can add date using DATE_ADD function:
DATE_ADD(date, INTERVAL 5 YEAR)
Check this similar question.
Or via PHP using DateTime modify method:
$dateTime = new DateTime($date);
$dateTime->modify('+ 5 years')->format('Y-m-d H:i:s');
I had one table listing events and attendies. To try Database normalization i split this into two tables.
I have made a database listing all the planned games for a team.
Table 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+name + organiser + date + location + cost + notes + id+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
i then have a second database that has everyone how has marked they will be comming
Table 2
++++++++++++++++++++++++++
+id + event + player +
++++++++++++++++++++++++++
The id is unique to each, The id from table 1 is used as the event in table 2.
i have a simple PDO query that pulls the data from table 1 into a HTMl table
if($db->connect_error) {
die("Connection error: ".$db->connect_error);
}
$sql = $db->query('SELECT * FROM event ORDER BY `date` ASC'
) or die($db->error);
echo"";
while($row = mysqli_fetch_assoc($sql))
{
$a = $row['attendees'];//will look up attendies to tick a check box if already aknowledged
$b = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
if (strpos($a,$b) !== false) {
$c = "checked='checked'";
}else{
$c = "";
}
$r=$row['id'];
echo "<div id='results'>";
echo "<CENTER>";
echo "<table BORDER=6 class='fixed'>";
echo "<TR> ";
echo "<TD COLSPAN=3 ALIGN=CENTER><form action='going.php' method='post' name='form".$r."'>Event ".$row['name']." i'm going
<input type='checkbox' name='going'".$c." onclick='document.form".$r.".submit ();'>
<input type='text' name='Organise' value='".$r."'>
<input type='text' name='name' value='".$b."'>
</form></TD>";
echo "</TR> ";
echo "<TR>";
echo "<td>";
echo "<B><u><font color='#0080FF'>Title </font></b></u>".$row['name']."<br></font>";
echo "<B><u><font color='#0080FF'>Orginiser </font></b></u>".$row['organiser']."<br></font>";
echo "<B><u><font color='#0080FF'>When </font></b></u>".date("D j-M-Y GA",$row['dt'])."<br></font>";
echo "<B><u><font color='#0080FF'>Location </font></b></u>".$row['location']."<br></font>";
echo "<B><u><font color='#0080FF'>Cost </font></b></u>£".$row['cost']."<br></font></TD>";
echo "<TD ROWSPAN=3 valign='top'><B><u><font color='#0080FF'>Attendies </font></b></u>".$row['attendees']."<br></font></TD>";//will change to table 2
echo "<TD ROWSPAN=3 valign='top'><B><u><font color='#0080FF'>notes </font></b></u>".$row['notes']."<br></font></TD>";
echo "</tr>";
echo "</table>";
echo "</CENTER>";
echo "</div>";
}
i have tried and joind these using
$sql = $db->query('SELECT t1.*, t2.event as t2event, t2.player as player
FROM `event` as t1
LEFT JOIN `going` as t2 on t1.id = t2.event
ORDER BY t1.`dt` ASC'
but all i got was a HTML table per event and per player. I'm sure its possible but can't work it out, can i create a html table from quering table 1 and add to attendies all those going from table 2 not just one or creating a result each one. ?
i've the following table:
Year, Name, Revenue, Qty
I would like a result table with
Name, Revenue2012, Qty2012, Revenue2013, Qty2013
How to do in Sql for MySql ?
Thanks
MySQL doesn't have a PIVOT function but you can get the result using an aggregate function with a CASE expression:
select name,
sum(case when year = 2012 then revenue else 0 end) revenue2012,
sum(case when year = 2012 then qty else 0 end) qty2012,
sum(case when year = 2013 then revenue else 0 end) revenue2013,
sum(case when year = 2013 then qty else 0 end) qty2013
from yourtable
group by name
If you're using php you can display it however you want:
$table=mysqli_query($con,"
SELECT *
FROM table
");
echo "<table>
<tr>
<th>Name</th>
<th>Revenue2012</th>
<th>Qty2012</th>
<th>Revenue2013</th>
<th>Qty2013</th>
</tr>";
while($row = mysqli_fetch_array($table))
{
echo "<tr>";
echo "<td>". $row['Name'] . "</td>";
echo "<td>". $row['Revenue'] . $row['Year'] . "</td>";
echo "<td>". $row['Qty '] . "</td>";
echo "<td>". $row['Revenue'] . $row['Year'] . "</td>";
echo "<td>". $row['Qty '] . "</td>";
echo "</tr>";
}
echo "</table>";
Or whatever suits your needs.
This question already has an answer here:
SQL Time exceeds
(1 answer)
Closed 9 years ago.
I've written a piece of code to calculate the waiting time for a patient as follows;
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H hours') as Waiting_Time FROM Patient";
Which displays the following data:
PatientID Forename Surname Gender Illness Priority Waiting Time
625 Max Courts M non-urgent moderate 4 hours
As the example shows; the waiting time is 4 hours;
I need a piece of code to detect and alert when a time is MORE THAN 4 hours; to echo an alert.
Example code I've tried;
if $waitingtime == >4 hours
then echo "patient must be seen!"
EDIT FULL CODE
<?php
$conn = mysqli_connect("localhost","root","") or die ("No connection");
mysqli_select_db($conn, "a&e") or die('Could not select database.');
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H hours') as Waiting_Time FROM Patient";
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'>
<tr>
<th>PatientID</th>
<th>Forename</th>
<th>Surname</th>
<th>Gender</th>
<th>Illness</th>
<th>Priority</th>
<th>Waiting Time</th>
</tr>";
while ($row = $result->fetch_object()){
echo "<tr>
<td>" . $row->PatientID . "</td>
<td>" . $row->Forename . "</td>
<td>" . $row->Surname . "</td>
<td>" . $row->Gender . "</td>
<td>" . $row->Illness . "</td>
<td>" . $row->Priority . "</td>
<td>" . $row->Waiting_Time . "</td>
</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
I'm using mysql_* for demonstation purposes. You should be using mysqli_* or PDO.
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time, TIME_FORMAT(ABS(TIMEDIFF(CURTIME(), Arrival_time)),'%H') as Waiting_Time FROM Patient";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row['Waiting_Time '] > 4)
{
echo "patient must be seen!"
}