Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Table articles
id - content - ins_dt
1 - lorem ipsum1 - 2013-01-09
2 - lorem ipsum2 - 2013-02-08
3 - lorem ipsum3 - 2012-11-03
4 - lorem ipsum4 - 2011-01-20
5 - lorem ipsum5 - 2011-12-13
6 - lorem ipsum6 - 2010-05-12
With query
SELECT ins_dt FROM articles ORDER BY ins_dt DESC;
I get this: (http://sqlfiddle.com/#!2/fc9ea6/5)
"2013 2013 2012 2011 2011 2010"
But I need this:
"2013 2012 2011 2010"
Whatever the values you are getting store them in array
then $finaldata = array_unique($yourarray);
You can use simple SQL query
SELECT GROUP_CONCAT(DISTINCT YEAR(ins_dt)) AS `year`
FROM articles
ORDER BY ins_dt DESC;
Here is the sample and demo: http://sqlfiddle.com/#!2/fc9ea6/3
As you have the data in an array, you can easily use Array unique in your code, which will give you an array with only unique values in it.
Alternately, you could change your SQL to only pull the data you need from the table itself like this:
select distinct year from (select date_format(ins_dt, '%Y') from tableArticles) myData)
try sort http://php.net/manual/en/function.sort.php
sort($myarr['ins_dt'], SORT_NUMERIC);
print_r($myarr);
a little searching effort could land you on great links like this one
http://php.net/manual/en/array.sorting.php
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a sample table name (info) in mysql database as follows:
id dateAdded
----------------------------
1 2013-12-24 03:03:19
2 2013-12-24 03:04:19
3 2013-12-24 03:06:14
4 2013-12-24 03:07:23
5 2013-12-25 03:04:19
6 2013-12-26 03:02:19
7 2013-12-26 03:03:19
I have another table name (error) as follows:
id date
----------------------------
11 2013-12-24 03:03:19
22 2013-12-24 03:04:19
33 2013-12-25 03:06:14
53 2013-12-25 03:04:19
62 2013-12-26 03:02:19
I want to COUNT how many ids from the two tables (info and error) with the same dates , so the result out will be :
date countinfo counterror
----------------------------------------
2013-12-24 4 2
2013-12-25 1 2
2013-12-26 2 1
Please if you could help me out with the Mysql query
Give this a try, maybe it can be written in a better way but this gives the desired output.
SELECT
DATE(Sub.dateadded) as `Date`,
( SELECT count(id)
FROM test.info
WHERE DATE(info.dateadded)=DATE(Sub.dateAdded)) as `CountInfo`,
( SELECT count(id)
FROM test.`error`
WHERE DATE(`error`.`date`)=DATE(Sub.dateAdded)) as `ErrorInfo`
FROM
(
SELECT `date` as dateadded
from test.`error`
UNION SELECT dateadded
FROM test.info
) Sub
GROUP BY Date(Sub.dateadded)
Notice that my database name used here is test, change that to your database name.
Since the date field is from different tables, you must UNION them in a subquery so that you can get the relevant dates. Then from there a simple subquery in the select is executed with the dateparameter.
In the future, try to name your tables with names that is not a datatype or function name etc, then the ` is not needed to wrap the database,table,column names
EDIT
If you want specific dates, just make use of WHERE.
Add this line before the GROUP BY
WHERE DATE(Sub.dateadded) IN ('2013-12-24','2013-12-25')
If you want between a time span you can do this
WHERE DATE(Sub.dateadded) BETWEEN '2013-12-24' AND '2013-12-30'
This will give the dates available between 24-30 of December.
Try this, please rename the table name that I have provided.
Info : Join both table get the count of distinct Id
select Date(dateadded) as datepart,
count(distinct(infotable.id)) as countInfo,
count(distinct(errortable.id)) as counterror from infotable inner join errortable
on Date(infotable.dateadded)=Date(errortable.date)
group by datepart
Try this for a pure MySQL approach:
SELECT dateAdded, COUNT(*) as count_info FROM info GROUP BY dateAdded;
SELECT date, COUNT(*) as count_error FROM error GROUP BY date;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I stored the month name in database as string which looks like
Apr-2013
May-2013
...
How could i sort the table month wise ?
Any help is appreciated.
You will have to format the date in order to sort it:
select aDate from t
order by str_to_date(aDate,'%b-%Y')
This is very inefficient, though. I'd recommend you to update that field and make it a date field or at least two ints: one for the month and one for the year. Then, if you need to get the name of the month you could use the monthname(date) function.
SELECT
*
FROM
dates
ORDER BY
STR_TO_DATE(date, '%b-%Y')
SQL Fiddle
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't find an answer to my question, it's probably easy but... anyway!
I have a database with every NHL game for one specific team in a table. Every game has been entered in the good order.
On my home page, I would like to display the upcoming game as well as the result of the previous game. How can I create a mySQL query to display the previous game based on today's date?
Thanks!
Do your game records have a timestamp or datetime value?
If so you could write a query ordering your games by the date smaller that now() and limit by one.
The query should look like this:
select * from games where gamedate < now() order by gamedate desc limit 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got a food menu which needs to have a pattern for when certain menus are displayed on a website.
For example, the pattern is split into yearly quarters, weeks and days.
**Quarter 1**
**Week 1**
Monday: 5
Tuesday: 4
Wednesday: 5
Thursday: 4
Friday: 6
Saturday: 7
Sunday: 7
The numbers represent the ID of the menu in the database.
So I have 4 quarters, 4 weeks and 7 days in the database. This is how it's laid out:
I don't get how to write the query to update the database, say for example I want to update the following:
I want to put Menu Number: 4, In Week 1, Quarter 1 on a Thursday? That is based off what they select on a HTML Checklistbox.
For your example the query could be something like this.
UPDATE menuset
SET thursday = 4
WHERE week = 1
AND quarter = 1
Something like this should work for you
update tablename SET monday=$monday WHERE week=$week AND quarter=$quarter
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As MySQL/PHP Noob I am struggeling to solve the following:
Table called "cars"
Fields called "modell" and "engine"
|Modell |engine
=====================
|Volvo |2,0
|Volvo |2,6
|Volvo |2,0
|Saab |1,8
|Saab |2,1
|Saab |1,8
|Saab |1,8
|Chevrolet |4,2
Would like to write a question and get as answer:
Here are:
2 volvo with 2,0 engine
1 volvo with 2,6 engine
3 saab with 1,8 engine
1 saab with 2,1 engine
1 Chevrolet with 4,2 engine
SELECT count(*) AS count,
Modell,
engine
FROM cars
GROUP BY Modell,engine
select modell, engine, count(*) as cnt
from cars
group by modell, engine
SELECT COUNT(*),Modell,ENGINE FROM cars GROUP BY modell,ENGINE