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;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to select the sum of different cites according to time column in my database. The required result will be as like below image.
Please have a look at the image.
AND finally I want to print the above data in my web page using PHP.
you can use a combination of SUM(CASE...) and GROUP BY clause. Something like this
SELECT
city,
SUM(CASE WHEN MONTH(orderDate)=1 THEN amount ELSE 0 END) as january,
SUM(CASE WHEN MONTH(orderDate)=2 THEN amount ELSE 0 END) as february,
SUM(CASE WHEN MONTH(orderDate)=3 THEN amount ELSE 0 END) as march
FROM orders
GROUP BY city
I'm assuming the table name is ORDERS and using the column orderDate to identify the mounth.
Good luck, bro.
If I understand you write, may be something like:
SELECT city, sum(amount)
FROM table_name
WHERE MONTH(date_column)=12
GROUP BY city;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've done a system for a client where they can nominate colleagues for doing a great job. Each person has 12 nominations to give annually.
So I've got a database with a table nominations which stores, id, nominator(id), nominated(id), reason and date.
I've also got a table user which stores user data such as, total nominations given, received, id, email etc.
So I'm creating a page from which you can pull reports.
Here you can choose a start date and end date and amount of records you would like.
How would the SQL query look to determine who made the most nominations between the specified dates?
I'm not a SQL guru at all...so any help would be appreciated very much.
After some research I've managed to find out COUNT(*) is the way to go...but don't want to run a query for every user that nominated between the specified dates...and sorting it this way could be a problem.
Please any help would be great.
select nominator, count(*)
from yourTable
where nominatedDate >= '1 Jan 2013' and nominatedDate <= '31 Jan 2013'
group by nominator
When you do aggregation functions (like COUNT. MIN, MAX, AVG) you either need to apply them to every row selected, which will give just one row in the output, or to GROUP BY items you want to make into sub-totals. In this case, for each value of Nominator in the table we get the Nominator value, and the count of rows containing that value.
The Where clause limits the counted rows to those where nominatedDate is in the given range. You can put AND and OR other tests (its already got one and).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Hey i need a bit mysql query help..
i have a table named airport_car_parking with 4 fields
id [int autoincrement primary key]
email [varchar]
departure_date [DATETIME]
arrival_date [DATETIME]
now i have two variables $departure_date [something in datetime format] and $arrival_date [something in datetime format]
I want to write a query in php - mysql that checks whether my booking days belongs into above table booking dates or not...
say if i consider my above table data like below
id | email | departure_date | arrival_date
1 | abc#gmail.com | 2013-12-22 12.15.00 | 2013-12-25 12.15.00
so no one can book a car in those 3 days of dec 2013
i have two variable like
$departure_date= 2013-12-24 12.15.00 and
$arrival_date= 2013-12-26 12.15.00
but a car can not be park in those days because table data shows that in this time already a car is booked... i need a query that checks any booking plot is available or not.
I write a query like below , but its valid only after existing period
$chk_date_sql="select * from airport_car_parking where return_date >= '$deperture_date'";
Your query is referencing return_date, which isn't a column in the airport_car_parking table.
As for the logic of the query, you want to make sure that the $departure_date isn't between any row's departure_date or arrival_date. I would recommend the following query -
$chk_date_sql="SELECT * FROM airport_car_parking WHERE '$departure_date' BETWEEN departure_date AND arrival_date;";
And then that query returns with any rows, that means that the requested time will not work.
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 have two MySQL tables with the following...
Apps
ID | Name
1 App1
2 App2
and...
Minutes
ID | AppID | Amount
1 1 10
2 1 15
3 2 35
I want to pull the Apps and display them by time used. Should look like this...
App2 35 minutes
App1 25 minutes
How can I do this?
Thank you.
You can use a JOIN with a GROUP BY. To add the amounts together use the SUM aggregate funcion.
SELECT name, SUM(amount)
FROM `apps`
JOIN minutes ON `apps`.`id` = `minutes`.`appid`
GROUP BY `apps`.`id`
Use a JOIN (SQL):
SELECT
Apps.Name,
Minutes.AppID,
SUM(Minutes.Amount)
FROM Minutes
INNER JOIN Apps ON Apps.ID = Minutes.AppID
GROUP BY Apps.Name, Minutes.AppID
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 have a table in mysql that has three column , one is the code that user bought it, the others is the time that this code has bought and last one is the buyers phone number; for exampale :
#CODE #phone #date
2154856265 3214490 2013-08-15
so now how can i make a query in php format that return me this statement :
i want the last 10 code that the user = 3214490 ordered by the date?
thanks for your patient ;)
You're kidding, right? This is about as trivial a query as they get.
select code
from table
where phone = 3214490
order by date desc
limit 10
You can try something like this:-
SELECT * FROM (
SELECT * FROM table where user = 3214490 ORDER BY date DESC LIMIT 10
) sub
ORDER BY date ASC
Assuming you're using 'phone' for the user id, you want a query like this:
SELECT * FROM [table_name] WHERE `phone` = 3214490 ORDER BY `date` DESC LIMIT 10
Replace [table_name] with the name of the table these records are stored in, as you didn't specify it in your question.
select c.code from directory c where c.phone= '3214490' order by c.date limit 0,10
This might help