I have a small problem with SQL. I need to select ID of rows and group them into arrays (or something) BY MONTH? I have a timestamp column there.
So if there are rows like this:
ID Timestamp
1 blalba(1.10.2017)
2 blabla(2.10.2017)
3 blabla(1.5.1996)
The output would be like
array(
[5.1996] => array([3]),
[10.2017] => array([1,2]);
)
(Or something like this).
Is this possible in PHP using some PHP libraries? Or Do I have to implement my own class doing this?
You are probably looking for group_concat
select group_concat(id separator ', ') as myList,
DATE_FORMAT(Timestamp, '%Y-%m') from <YOUR_TABLE>
GROUP BY DATE_FORMAT(Timestamp, '%Y-%m');
Well you might be handle this on the MySQL side by ordering by month/year:
SELECT
ID, Timestamp
FROM yourTable
ORDER BY
DATE_FORMAT(Timestamp, '%Y-%m');
This query would return a result set to PHP which would be ordered such that all records in the same month and year would be clustered together. You could then just iterate this result set and process the records as you want.
Related
i have one table collecting scores and other informations like the date and the user id. I would like to get the MAX of the current month and the other fields of the row. I'm having a problem to get the other informations since with functions we cannot get other fields.
I think i should do an inner join but i don't how to make it.
Thank you.
Your question is rather vague. But if you want one row, then the idea for the solution is order by and fetch first row only.
In standard SQL, the query would look like:
select t.*
from t
where extract(year from datecol) = extract(year from current_date) and
extract(month from datecol) = extract(month from current_date)
order by t.score desc
fetch first 1 row only;
Databases often differ on database functions. For instance, many use year() and month() functions, rather than extract(). Similarly, many databases do not support fetch first 1 row only, using limit or select top instead.
Thank you, yes it does work doing this way but the idea was to use MAX (for learning purpose).
I have scores table, with the following fields: id, score, date, user_id
I would like, using MAX, to get the best and latest score of the current month along with the other fields (ie the id, date and user_id).
I have a table where the dating is not standard and need to somehow organise the rows by date and time.
job_date | job_time
=========================
12/12/2012 | 10.30am
11/10/2012 | 9.00pm
14/11/2012 | 11.50pm
Is there any way of formatting these within mysql. I have looked at the DATE_FORMAT() function but the examples I have found don't seem to relate to the format within my tables.
SELECT *, STR_TO_DATE(CONCAT(job_date,' ',job_time), 'Y-m-d H:i:s') AS date_format from table ORDER BY date_format DESC
The key method is STR_TO_DATE.
I will give you two solutions :
first : if you don't want to change your database :
SELECT jobdate, STR_TO_DATE(job_time,'%h:%i%p')AS real_job_time FROM yourtable ORDER BY real_job_time;
second : if you can modify your database, use the TIME format :
ALTER TABLE yourtable
MODIFY COLUMN job_time TIME NOT NULL;
UPDATE yourtable SET job_time = STR_TO_DATE(job_time,'%h:%i%p');
and to select
SELECT jobdate,job_time FROM yourtable ORDER BY job_time
I think the second solution is by far the best and that you should choose it.
Obviously storing it as a correct date would be a lot better and result in quicker queries.
SELECT *
FROM table
ORDER BY substr(job_date, -4), substr(job_date, 4, 2), substr(job_date, 1, 2)
I have a MySQL table with over 200 values. One of the columns on my table is 'date'. Out of all 200 values there are only 5 unique dates.
How can I list out the unique values of the dates and echo them with php. e.g. not getting back 200 instances of dates but just 5.
Use DISTINCT
SELECT DISTINCT `date` FROM `tablename`....
SELECT myDate FROM myTable GROUP BY myDate
Or...
SELECT DISTINCT myDate FROM myTable
DISTINCT is a nice short hand, but if you ever then want to make use of the query for other purposes, if often constrains you a bit too much. So I prefer the GROUP BY version.
I have a table with that contains information that I would like to show stats for each day between a particular range of dates. The table consists of the following:
CREATE TABLE IF NOT EXISTS `questions` (
`qid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`question_created_date` datetime NOT NULL,
`question_response_date` datetime NOT NULL,
)
I am able to use the following query to displays the amount of questions created on each day between $start_date and $end_date.
SELECT COUNT(*) as q_count, DATE(question_created_date) as date FROM questions where question_created_date between "$start_date" and "$end_date" GROUP BY date ORDER BY date asc
I would also like for it to list the amount of topics that were responded to on the same days. For exmaple if on 7/5/11 there were 10 new questions and 5 questions responded to and on 7/6/11 there were 5 new questions and 11 questions responded to, I want to be able to show:
Date|New|Responded
7/5/11 10 05
7/6/11 05 11
Can this be done via mysql?
I know I can perform the query listed above and then for each day perform another query to get the amount of questions responded to for that day, but I was wondering if there was an easier and more efficient way to do this in one query.
Thanks in advance.
The two dates aren't related to each other, except that logically, the response date could only ever be greater than the created date. Grouping on one date will destroy any information you could extract from the other date, so in other words - you'll need two queries.
The fields returned would be the same - a count and a date field, so you could issue this is as a single query call, but it'd have to be done as a union query:
SELECT 'created' as src, count(*) as cnt, DATE(question_created_date) as created ...
UNION
SELECT 'answered' as src, count(*) as cnt, etc...
The virtual 'src' column will tell you which internal query produced the result, and the union syntax lets you issue this all as a single query. But within MySQL, it'd still be treated as two completely separate queries, whose results just happen be returned all at once.
You could do something like this using UNION. MySQL possibly has a built in operator that is more efficient, but this will work too:
select main.date, max(qcd), max(qrd) from
(SELECT DATE(question_created_date) as date, count(*) as qcd, 0 as qrd FROM questions
where question_created_date between "$startdate" and "$enddate" GROUP BY date
union
SELECT DATE(question_response_date) as date, 0, count(*) as qrd FROM questions
where question_response_date between "$startdate" and "$enddate" GROUP BY date)
main group by main.date order by main.date asc;
I have a question about constructing a MySQL query. I have a table with one column containing values, and another column containing timestamps. What I'd like to do is get the number of distinct (unique) values for a field from a specific epoch up until various points in time so that I can plot the number of unique values over time. For example, I'd like the query result to look like the following:
Date, COUNT( DISTINCT col1)
2011-02-01, 10
2011-02-02, 16
2011-02-03, 24
etc.
Note that these values are the number of distinct values starting the same point in time. Currently to accomplish this, I'm using a loop in PHP to iterate a single query for each date and it takes forever since I have a large DB. To give a better picture, the inefficient code I'd like to replace looks like the following:
for($i=0;$i<count($dates),$i++){
$qry = "SELECT COUNT (DISTINCT `col1`) FROM `db`.`table` WHERE `Date` BETWEEN '".$EPOCH."' AND '".$dates[$i]."';";
}
Any help would be appreciated.
Thanks
If understood your question, you can use a GROUP statement:
SELECT StampCol, COUNT(DISTINCT DataCol) FROM MyTable GROUP BY StampCol
SELECT DATE_FORMAT(date_column, "%Y-%m-%d") AS date_column, COUNT(visitors) AS visitors FROM table GROUP BY DATE_FORMAT(date_column, "%Y-%m-%d") ORDER BY date_column desc"