Filling in the date gaps mysql query for ajax call [duplicate] - php

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";

Related

Search for a string in an array through SQL [duplicate]

This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 6 years ago.
I have a table named 'offers' that has a column in_stores. It contains the ids of the stores that this offer is available in. Graphically this table looks like this:
id | title | in_stores
1 | Shoes | 1002,1003
2 | Gloves | 1020,1011
3 | Shades | 1002,1009
4 | Hats | 1010,1002
5 | Shoes | 1220
6 | Shirts | 1010
7 | Hats | 1002
Each value in in_stores is saved with the implode() function through PHP.
My question:
How to select in a single mysqli query all the offers that are available in a store with id 1002. In this example the query should return offers with id 1,3,4,7. I guess I should use something like explode() first to get the results in an array and than in_array() to search for the specific id in it but all those functions are unavailable within SQL.
This is a major issue with storing comma separated fields.
MySQL does have a function to allow searching for these, and this will be a lot more reliable than trying to rely on using LIKE. But as it still cannot effectively use any index it will not be fast.
SELECT *
FROM offers
WHERE FIND_IN_SET('1002', in_stores)
Far better to redesign your database to use an extra table with a row for each value for each id.
You "could" do it with a LIKE Query, but this is not recommended.
SELECT * FROM offers WHERE in_stores LIKE "%1002%".
As long as you don't have values bigger than 9999 this will work.
BUT when one of your stores has the id 11002 of 99991002 it will also return these unwanted values.
What you should do is transform your mysql table to a have a second table storeLocations or sth. else. This should only have the properties offer_id and store_id.
It will transform your data to:
`offers`
id | title
1 | Shoes
2 | Gloves
3 | Shades
4 | Hats
5 | Shoes
6 | Shirts
7 | Hats
`storeLocations`
offer_id, store_id
1 | 1002
1 | 1003
2 | 1020
2 | 1011
3 | 1002
3 | 1009
4 | 1010
4 | 1002
5 | 1220
6 | 1010
7 | 1002
Then you can select from it like
SELECT * FROM offers AS o
LEFT JOIN storeLocations as l ON (o.id=l.offer_id)
WHERE l.store_id = 1002;
Now when you insert data you don't have to use implode but insert as many rows into storeLocations as there are store_id's for that specific item.
For more info on that topic have a look here.
I think you should be using the MySQL LIKE operator. It searches the database column for strings specified in your query.
Try this query:
$sql = "SELECT * FROM offers WHERE in_store LIKE '%1002%'";
Check this link for more explanation: W3Schools - SQL LIKE Operator
I think that should do the trick.

MySQL Search for newest entry for specific column cell strings [duplicate]

This question already has answers here:
Select row with most recent date per user
(14 answers)
Closed 8 years ago.
I'm very new to MySQL and PHP, and I want to make my page tell me the newest entry for each name, but not just the newest entry for the table.
The table looks something like this,
55 | Curtis | present | 2014-06-22
87 | Curtis | present | 2014-06-22
56 | James | absent | 2014-08-25
57 | Curtis | late | 2014-08-25
48 | Will | present | 2014-08-25
47 | James | present | 2014-08-18
43 | Will | present | 2014-08-18
I want it to output like this,
43 | Will | present | 2014-08-25
47 | James | present | 2014-08-25
57 | Curtis | late | 2014-08-25
Would anybody be able to help me?
Assuming your id is incremental (that is, more recent records have bigger id values), this can be solved by using a nested query that gets the max values of id for each name:
select a.*
from yourTable as a
inner join (
select max(id) as max_id
from yourTable
-- Add any WHERE conditions here (for example: WHERE status='present')
group by name
) as b on a.id = b.max_id
-- WHERE conditions go here
-- ORDER BY columns go here

Selective print of MySQL results for multi-line chart

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

MySQL Conditional Select Replace Query

I have found myself in need of replacing certain returned results from a MySQL Select Query. I have the following table and data (simplified for example purposes)
uid | duration | range | unique | stamp
-----------------------------------------------------------------
23 | d | 43 | 1 | 1
24 | d | 65 | 0 | 2
25 | d | 76 | 0 | 3
26 | d | 33 | 0 | 4
27 | d | 44 | 1 | 5
28 | d | 43 | 1 | 6
29 | d | 67 | 0 | 7
30 | d | 88 | 0 | 8
31 | d | 63 | 0 | 9
The stamp column is what I want to do the replace on. Rather than a simple text replace, I was wondering if its possible to run some sort of user defined function on the column and replace it dynamically.
For example If the data returned in the stamp column is a 1, I would like it to replace it with today's timestamp, if it is a 2 then yesterdays timestamp, a 3, the day before yesterdays and so on and so forth.
So my question is, is it possible to point REPLACE to a function that processes the value and then returns what to replace it with. Or if not, is there another way to accomplish this.
I could obviously post process the returned data in PHP and make the changes, but with millions of records returned, it will increase the load time considerably.
EDIT TO MAKE THINGS A BIT CLEARER: I want to replace the stamp column in the data returned from a SELECT Query, I am not storing the data anywhere, or replacing the data in the table. The table will remain unchanged.
Thanks
Absolutely possible:
UPDATE stamps
SET stamp = CURRENT_DATE - INTERVAL stamp - 1 DAY;
Fiddle here. Note that you have to decrement the stamp value by 1 to "minus 0 days" for a stamp of 1. If you remove the - 1, you'll end up storing yesterday's date for stamp values of 1.
UPDATE to answer your question about doing it on SELECT:
SELECT CURRENT_DATE - INTERVAL stamp - 1 DAY
FROM stamps;
Updated Fiddle here
You can use the case statement of mysql
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
If you are just trying to retrieve the results, use the following:
SELECT CURDATE() - INTERVAL stamp -1 DAY FROM myTable

Fixture generator in PHP or MySQL

I am trying to generate fixtures for a sports website. I have a table called Members, with relevant columns being member_id and league_id. The league_id will be passed from a form on the previous page as the variable $leagueid.
I'm pulling out all the member ID's relating to that league ID using...
$result = mysql_query("SELECT member_id FROM Members WHERE league_id = '$leagueid'")
I now need to generate fixtures for all these member ID's and then insert that data into the MySQL table 'Fixtures'. Each row of data in that table needs to include:-
player1 - member_id of the first player
player2 - member_id of the second player
week - integer showing which week the match will be played
league_id
However, there are some special conditions that also need to be applied.
Every 3rd week needs to remain free (i.e. weeks 3,6,9,12 etc). No matches can be scheduled on these weeks
There needs to be an option (which will be selected and passed from a form on the previous page as a checkbox variable called $double) which will double up the matches. This means that after generating one complete round of fixtures, you need to take the generated list, swap ID's for players 1 and 2, and duplicate them all. So 1 round of fixtures could look like this....
Week 1
1 vs 2
3 vs 4
Week 2
1 vs 3
2 vs 4
Week 3
1 vs 4
2 vs 3
Then you would swap the ID's and add on another set...
Week 4
2 vs 1
4 vs 3
Week 5
3 vs 1
4 vs 2
Week 6
4 vs 1
3 vs 2
What I'm looking for is some code that will generate all these fixtures while keeping in mind all the special conditions that I've listed.
I know this is all possible in PHP but I also think I can do it using one SQL query instead, which might be a lot cleaner. Can anyone help me out?? Thanks!!
P.S. I know I'm using mysql and not mysqli. I am currently trying to transfer over to mysqli but I'm having some problems which I have posted on a separate question that I have yet to get a correct answer to.
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT * FROM ints WHERE MOD(i,3) > 0;
+---+
| i |
+---+
| 1 |
| 2 |
| 4 |
| 5 |
| 7 |
| 8 |
+---+
The second part is simply
INSERT INTO my_table SELECT week_id+3, column_2, column_1 FROM my_table;

Categories