hi I'm trying to create a chart with php and mysql to show daily number of posts that generate Automatically using mysql queries.in database just we have date column.
I want some thing like this
date | posts count
------------+---------------
2017/05/02 | 5
2017/05/03 | 0
2017/05/04 | 1
2017/05/05 | 2
2017/05/06 | 0
2017/05/07 | 3
how to do this?
First of all you need to learn how to use these two:
Group by
COUNT
Since I have no idea about your table architecture here is a sample that you can adjust to your case:
select p.datePost, count(p.id) as postcounts
from posts p
group by p.datePost
Related
I am using backpack-crud controller for
PHP-Laravel.
With the crudController given by backpack (library), all I have to do
is to query it with Laravel Eloquent (also raw sql is possible) queries.
Than the Backpack library will automatically
print the listview for me.
But I am struggling with this difficult query.
The thing is that I have 4 columns,
session_id | column_id | batch | data
10 | 1 | 1 | data1
10 | 2 | 1 | data2
10 | 1 | 2 | data1*
10 | 2 | 2 | data2*
Let's say this is the data I have.
I want to display this grouping by session_id, batch,
and ordering within row by column_id.
so the result query would be something like
1 : data1 data2
2 : data1* data2*
If there is a third batch with data
session_id | column_id | batch | data
10 | 1 | 3 | data1**
Then it would appear under the third batch as
3 : data1**
I can do this with code but not with sql.
Any advice would be grateful.
This looks like a PIVOT in sql server. Unfortunately mysql does not have this feature.
I can give you an approximate raw mysql query using GROUP_CONCAT. Assuming your table name is mytable.
SELECT
session_id,
batch,
GROUP_CONCAT(data ORDER BY column_id SEPARATOR ', ') AS dataList
FROM mytable
GROUP BY session_id, batch
Then you can split the aliased dataList column using given separator (here I've used ,).
You may change the separator according to the data contain in data column as you wish.
Hope this helps to you.
This question already has answers here:
What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?
(9 answers)
Closed 5 years ago.
What is the best way to include empty dates for a time series graph generated from a mysql db.
If I run a query to get say all of the red cars sold on a particular day I could run :
SELECT count(car_sale_order) as 'count', DATE(sale_date) as 'sale_date'
FROM car_sales
WHERE colour = "red";
but the results could have date holes in them i.e.
------------------
count | sale_date
------------------
2 | 2017-09-03
10 | 2017-09-04
1 | 2017-09-07
23 | 2017-09-09
45 | 2017-09-10
2 | 2017-09-11
21 | 2017-09-12
when what id really like is :
------------------
count | sale_date
------------------
2 | 2017-09-03
10 | 2017-09-04
0 | 2017-09-05
0 | 2017-09-06
1 | 2017-09-07
0 | 2017-09-08
23 | 2017-09-09
45 | 2017-09-10
2 | 2017-09-11
21 | 2017-09-12
I use PHP so know that i could generate this stuff at that side but it would be really handy to just have the result set include this from the get go....
This isn't the same as What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?
I'm looking for a MySQL only result
Consider using a calendar table. I have always found this to be the most reliable way to create date reports as you have a row for every date which you join with your table. In your case, you may wish to use something like:
SELECT count(car_sale_order) as 'count', DATE(sale_date) as 'sale_date'
FROM calendar_table
LEFT JOIN car_sales ON calendar_table.date = car_sales.sale_date
WHERE calendar_table.date BETWEEN '2017-01-01' and '2017-01-31' and colour = "red";
ID | PID | NAME | VALUE |
-------------------------------------
60 | 1 | Test1 | 9999 |
21 | 2 | Test2 | 9999 |
44 | 1 | Test3 | 9999 |
37 | 4 | Test4 | 9999 |
24 | 1 | Test5 | 9999 |
Hey all!
I am kind of new to PHP and DBs so I really dont know how to start with this.
So I want to want to make a sorting inside a DB where the IDs differ too much.
(that means that the first ID starts with 34 and the next one is something like 43 next is 55 etc.)
I have a table which looks like the one above.
Now what I would like to do is changing the values in the column VALUE depending on the values which are in PID.
This means that if in PID the value equals 1 the VALUE on the same row should become 1001 and for the next one 1002, next 1003.
If PID = 2 then VALUE should be changed to 2001 then 2002 then 2003 etc.
This would be for an already existing table but I would also like to include the VALUE values everytime I add a new statement into that table.
So a simple check in pseudocode:
If PID equals 1
then check VALUE column for the highest number that starts with "1"
make it +1 and add it into the column of that row
Is that possible to do?
what would you guys suggest me to do instead (to make things easier)?
If you need further info, tell me please and I will try to explain things better, I dont know if my explanation says what I'm trying to do.
Thank you in advance.
Cheers,
K.
You can use UPDATE .. JOIN and join to a derived table containing the "rank" of each ID , and update accordingly :
UPDATE YourTable t
JOIN(SELECT s.ID,s.PID,COUNT(*) as cnt
FROM YourTable s
JOIN YourTable s2
ON(s.pid = s2.pid AND s.id >= s2.id)) p
ON(t.id = p.id)
SET t.value = (1000*t.pid) + p.cnt
The inner query here basically "ranks" the data by a self join. It joins to it self by the condition s.pid = s2.pid AND s.id >= s2.id , in words - Same PID that happen before me including me, so the first one will join to 1 record, the second to two and so on.. Then you just update value column to pid*1000 , plus the rank.
I need to make a googlechart print 4 lines that will represent the evolution of temperature of 4 sensors that store their readings in MySQL db.
My schema is:
id | datetime | node_id | temperature
if I run the query:
SELECT * FROM sensor_readings ORDER BY id DESC LIMIT 4
the result is like:
4 | 21-5-2014 17:00 | 3 | 18.6
3 | 21-5-2014 17:01 | 1 | 18.5
2 | 21-5-2014 17:02 | 4 | 18.7
1 | 21-5-2014 17:04 | 2 | 18.2
For on node I could just query the results - WHERE node_id = 1
Now for four nodes how can I make my query in order to be able to echo a code like
[date, temperature (of node1), node1, temperature (of node2), node2
....]
So that google chart is able to print my graph? I've made several attempts to load results in temporary arrays in php, without luck. Any idea how I could approach this issue?
Thanks a lot!
try with adding group by
SELECT * FROM sensor_readings ORDER BY id DESC GROUP BY datetime;
after that, you can use ajax to collect the result of the query as response.
then put the response into a or or what you want
I am trying to build an archive list for a blog using php and mysql. The problem is I am not sure of the best way to do this.
I was hoping there was a way to get a list of years and than display them so lets say my table has id | year | content and has the current rows
1 | 2013 | content
2 | 2013 | content
3 | 2013 | content
4 | 2012 | content
5 | 2012 | content
6 | 2011 | content
is it possible to make a mysql statement that well only return IDs 1,4,6, if so how?
try this
select * from table
group by year
if you consider the order then add this in the end order by id
DEMO here
you can also get the specified Id by using Min or Max. like this
select min(id) ,year ,content from table
group by year
demo
select min(id) , year from table group by year
To just get a list of the 'year's in your table:
select distinct year from table
Depending on the size of your table and how often you're running the query, you could think about indexing on 'year'.