I have this mysql Table:
+--------------------+---------+-------+
| date | query | count |
|--------------------+---------+-------|
|2012-11-18 09:52:00 | Michael | 1 |
|2012-11-18 10:47:10 | Tom | 2 |
|2012-11-17 15:02:12 | John | 1 |
|2012-11-17 22:52:10 | Erik | 3 |
|2012-11-16 09:42:01 | Larry | 1 |
|2012-11-16 07:41:33 | Kate | 1 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and so on. I can simply take results and order them by date in one row via this code:
$queries = mysql_query("SELECT * FROM my_tables ORDER BY date DESC LIMIT 20");
while($row = mysql_fetch_array($queries)){
echo "Name ".$row['query']."";
}
But how to display elements from table ordered by specific date like this:
In 2012-11-18:
Michael
Tom
In 2012-11-17:
John
Erik
In 2012-11-16:
Larry
Kate
and so on. Thanks!
Here is teh PHP code:
$query = mysql_query("SELECT date, query FROM table6 ORDER BY date DESC LIMIT 20");
$group_date = null;
while ($row = mysql_fetch_assoc($query)) {
if ($group_date !== substr($row["date"], 0, 10)) {
$group_date = substr($row["date"], 0, 10);
echo "<h1>$group_date</h1>\n";
}
echo "${row['query']}<br>\n";
}
Output:
2012-11-18
Tom
Michael
2012-11-17
Erik
John
2012-11-16
Larry
Kate
Note that while this code "groups" rows by one column, it can easily be extended to group rows by multiple columns. Left as an exercise.
+1 good question, THIS QUESTION IS NOT SIMPLY HOW TO ORDER A QUERY!!
I think this can be done using GROUP_CONCAT function. this will combine the matching results when you group them and seperate them by commas. ONce you have the results you can explode or do whatever you want
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
Your going to have to convert and group by date, and group_concat the names. then (in php or whathaveyou) explode the names by commas, and echo the date followed by the names for each result.
edit Looks like you wanted a PHP way to solve this? oops
Use this:
SELECT * FROM my_tables WHERE date > "2012-11-16" ORDER BY date LIMIT 4
Try the WHERE statement:
select * from my_table where date_column > "2012-11-18 00:00:00" and date_column < "2012-11-18 23:59:59" order by date_column
What about
SELECT * FROM my_tables GROUP BY YEAR(date), MONTH(date), DAY(date) ORDER BY date DESC LIMIT 20
Related
So I've been following this tutorial: https://www.plus2net.com/php_tutorial/chart-line-database.php
I am trying to add a line chart to my website to display number of sales for each month.
This is an example on how my SQL table looks like:
| ID | user | sale_id | date |
| 1 | RVN4372 | 1341234 | 2020-09-22 17:31:32 |
| 2 | OVI6517 | 5452351 | 2020-09-22 15:14:43 |
| 3 | RVN4372 | 8452176 | 2020-09-17 16:23:54 |
| 4 | FOK8905 | 7421312 | 2020-09-17 11:23:11 |
| 5 | DIF9127 | 4236123 | 2020-09-15 15:32:26 |
This is how my current query looks like:
<?php
if($stmt = $link->query("SELECT user,COUNT(*) FROM sales WHERE yearweek(DATE(date), 1) = yearweek(curdate(), 1) GROUP BY user order by COUNT(*) DESC")){
$php_data_array = Array(); // create PHP array
while ($row = $stmt->fetch_row()) {
$php_data_array[] = $row; // Adding to array
}
}else{
echo $link->error;
}
//print_r( $php_data_array);
// You can display the json_encode output here.
echo json_encode($php_data_array);
// Transfor PHP array to JavaScript two dimensional array
echo "<script>
var my_2d = ".json_encode($php_data_array)."
</script>";
?>
<div id="curve_chart"></div>
This is how it looks like on my website:
So this basically groups the users, and count how many sales each user has. On the X axis is display the user's name, and Y axis total number of sales.
I want to change this, so in the X asix is display the month, and Y asis total number of sales. How can I accomplish this?
EDIT: Hava been trying out some, but can't make it work. This is what I've got so far:
if($stmt = $link->query("
SELECT YEAR(date)
as SalesYear,
MONTH(date) as SalesMonth,
COUNT(*) AS TotalSales
FROM sales
GROUP BY YEAR(date), MONTH(date)
ORDER BY YEAR(date), MONTH(date)
AND COUNT(*) DESC
")){
If you have more than one year then you need to group in Year-month combination. Then change this query to this.
SELECT CONCAT(YEAR(date),'-' MONTHNAME(date)) as ym, COUNT(*) FROM sales GROUP BY ym ORDER BY count(*) DESC
I have a database like this:
+----------+----------+------+
| username | password | time |
+----------+----------+------+
| a | b | 1234 |
| c | d | 5678 |
| e | f | 9012 |
+----------+----------+------+
Now I have to arrange this by time so I ran:
mysql_query("SELECT * FROM `table` ORDER BY `time`")
This showed me rows arranged by time in ascending order, but I have to get only username c and e or I have to get the last two rows from the query.
I have tried:
mysql_query('SELECT * FROM `table` ORDER BY `time` LIMIT 2')
But this showed me usernames a and c but I have to get the last two, how to do that?
SELECT * FROM table
ORDER BY time DESC
LIMIT 2
SELECT * FROM table ORDER BY time DESC LIMIT 2
Adding the DESC keyword will sort the results in descending order.
Try this:
SELECT * FROM table ORDER BY time DESC LIMIT 2
Try
mysqli_query('SELECT * FROM table ORDER BY time DESC LIMIT 2')
Assuming you have an id field with AUTO_INCREMENT set.
To get the last two from the table.
SELECT * FROM table
ORDER BY time DESC
LIMIT 2
I have spent a week trying to figure this out, it is somewhat working by combining things from various sources but not fully working yet.
Basically I have an orders table which I'm trying to group customers by their first order date, then show the total spent by this group up to now.
This is my SQL query:
SELECT DISTINCT email, billing_name,
FORMAT(SUM(total),2) AS total,
DATE_FORMAT(MIN(orderdate), '%Y-%m') AS firstorder,
DATE_FORMAT(MAX(orderdate), '%Y-%m') AS lastorder
FROM orders
GROUP BY email
ORDER BY firstorder ASC
and with PHP I am doing:
$rows = array();
while($row = mysql_fetch_array($query))
$rows[] = $row;
foreach($rows as $row) {
$currMonth = $row['firstorder'];
$total += $row['total'];
if ($currMonth != $prevMonth) {
echo $currMonth.' = $'.$total';
$prevMonth = $currMonth;
$total = 0;
}
}
this gives me a list like:
2010-05 = $230.49
2010-06 = $557.32
2010-08 = $223.38
but the numbers don't add up, what am i doing wrong? and how can I display how much a group have spent in other months? this is how I eventually want to show the data, http://www.quickcohort.com/
Please help! thanks!!
Depends on what you're really after and what your data looks like.
If data looks like:
email |BILLING NAME|Total |OrderDate
----------------------------------------------
john#gmail.com |John Smith |200.00 |15/05/2010
john#gmail.com |John Smith | 15.49 |19/10/2010
john#gmail.com |Rico Swavez | 15.00 |10/08/2010
jane#gmail.com |Jane Doe |250.00 |23/06/2010
jane#gmail.com |Jane Doe |307.32 |27/10/2010
juan#gmail.com |Juan Valdez |223.38 |30/08/2010
Then...
SELECT email, billing_name,
FORMAT(SUM(total),2) AS total,
DATE_FORMAT(MIN(orderdate), '%Y-%m') AS firstorder,
DATE_FORMAT(MAX(orderdate), '%Y-%m') AS lastorder
FROM orders
GROUP BY email, billing_name
ORDER BY firstorder ASC
Will return
EMAIL | BILLING NAME |TOTAL |FIRSTORDER | LASTORDER
------------------------------------------------------------
john#gmail.com | John Smith |215.49|2010-05 | 2010-10
jane#gmail.com | Jane Doe |557.32|2010-06 | 2010-10
john#gmail.com | Rico Swavez | 15.00|2010-08 | 2010-08
Juan#gmail.com | Juan Valdez |223.38|2010-08 | 2010-08
Run your query first in mysql are you getting the results you want? if not, then the problem is in the SQL, not the PHP. If the SQL is returning what you want, then the problem is in the PHP
The following query should do the trick:
SELECT
FORMAT(SUM(z.`totalSpent`),2) AS cohortRevenueToDate,
z.`firstOrderMonth`,
GROUP_CONCAT(`email`)
FROM
(
SELECT
`email`,
SUM(`total`) AS totalSpent,
DATE_FORMAT(MIN(`orderdate`), '%Y-%m') AS firstOrderMonth
FROM `orders`
GROUP BY `email`
) AS z
GROUP BY z.`firstOrderMonth`
ORDER BY z.`firstOrderMonth` ASC
I have included a GROUP_CONCAT in case you are interested on each cohort composition.
I have two field name $date and $time.
date | time
2011/01/09 | 08:22:25
2011/01/09 | 13:00:55
2011/01/09 | 17:45:18
2011/01/09 | 17:30:26
2011/01/08 | 18:22:00
2011/01/08 | 12:06:39
How to let the newest before the oldest. I want them to be:
date | time
2011/01/09 | 17:45:18
2011/01/09 | 17:30:26
2011/01/09 | 13:00:55
2011/01/09 | 08:22:25
2011/01/08 | 18:22:00
2011/01/08 | 12:06:39
How to write a select * from article order by...desc...? Thanks.
SELECT * FROM yourtable ORDER BY date desc, time desc
select * from article order by date desc, time desc
SELECT * FROM article WHERE 1 ORDER BY `date` DESC, `time` DESC;
should do just fine.
$sql="SELECT * FROM CAFTERIA ORDER BY sl_no DESC;"
After reading the answer to this question and a bit of pain I discovered the following worked for me. This answer includes a PHP example for SELECT, WHERE, ORDER BY and LIMIT. They need to be in that ORDER with LIMIT last of it doesn't work.
$query = "SELECT * FROM Page_Relations WHERE Child=$article_id ORDER BY Page_ID ASC LIMIT 0 , 30 ";
I wanted to create a context sensitive menu list where the pages in the index are ordered by a Page_ID number.
Lets say I have the database table setup in this fashion,
ID | Name | Area | Timestamp
---+-------+------+------------
1 | Hill | 1 | 1293243080
2 | Sam | 1 | 1293243084
3 | Joe | 2 | 1293243087
4 | Bob | 2 | 1293243089
5 | Matt | 3 | 1293243091
6 | Billy | 3 | 1293243095
Then I wish to return the Name of the person with the largest Timestamp and with a certain Area number.
However, when I try to return for example the Name Bob I only get Billy because he has the largest Timestamp of everyone.
How can I get the php to select not only the person with the max Timestamp, but the person with a certain Area number also?
This is my code so far -
(I am looping it because I am displaying the name of the person with the largest Timestamp in each Area)
for ($t = 1; $t <= 3; $t++){
$result = mysql_query("SELECT * FROM forum_posts WHERE Area='$t' AND Timestamp=(select max(Timestamp) from forum_posts)");
while($row = mysql_fetch_array($result))
{
$post_name[$t]=$row['Name'];
}
}
print_r ($post_name);
What do you guys suggest I do?
$result = mysql_query("SELECT * FROM forum_posts WHERE Area='$t' AND Timestamp=(select max(Timestamp) from forum_posts where Area='$t')");
a better option would be.
$result = mysql_query("SELECT * FROM forum_posts WHERE Area='$t' ORDER BY Timestamp DESC LIMIT 1");
use this
$result = mysql_query("SELECT * FROM forum_posts WHERE Area='$t' ORDER BY Timestamp DESC LIMIT 1");
You need a GROUP BY on your query.
You need a OR Condition. The query would be:
$result = mysql_query("SELECT * FROM forum_posts WHERE Area='$t' OR Timestamp=(select max(Timestamp) from forum_posts)");
SELECT * FROM forum_posts (
SELECT
RANK() OVER (ORDER BY timestamp ASC) AS ranking,
name
FROM person
WHERE area = 1
) AS foo
WHERE ranking = 1