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;
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 6 years ago.
Improve this question
I have a piece of code below and according to a condition I wanted to know why the question is invalid
CREATE VIEW HotelBookingCount (hotelNo, bookingCount)
AS SELECT h.hotelNo, COUNT(*)
FROM Hotel h, Room r, Booking b
WHERE h.hotelNo = r.hotelNo AND r.roomNo b.roomNo
GROUP BY h.hotelNo;
The question is :
SELECT hotelNo
FROM HotelBookingCount
WHERE bookingCount > 1000;
When I type it shows error
What is the logic behind this? Why I am not supposed to do this?
You can't have VIEWs that way and also there is nothing to pass the arguments as they are unused. Try with -
CREATE VIEW HotelBookingCount
AS SELECT h.hotelNo, COUNT(*) bookingCount
FROM Hotel h, Room r, Booking b
WHERE h.hotelNo = r.hotelNo AND r.roomNo b.roomNo
GROUP BY h.hotelNo;
Then
SELECT hotelNo
FROM HotelBookingCount
WHERE bookingCount > 1000;
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.
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 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 want to retrieve content content based on their time stamp. And my query is
SELECT link,title,timestamp,photo,link,author,comments FROM posts
WHERE timestamp='*Todays Date -1*' LIMIT 6
If the query returns zero rows, how can I query for results 2 days back or three days back using a while statement?
Why not just query full table (without timestamp filter) and return limit of 6 rows. in this case there would be no reason to iteratatively query
SELECT link,title,`timestamp`,photo,link,author,comments
FROM posts
ORDER BY `timestamp` DESC
LIMIT 6
This gives you up to 6 records with most recent records returned first. You should have index on timestamp field for this query, which is OK because you should already have one because you were previously trying to filter on this field.
If you have a case where more than 3 rows show up within last day and you don't need to show records from previous days, this is easy enough to achieve by inspecting the values as you loop through the result set.
This also prevents you from getting into an infinite loop if you only have 2 records in the database.
Also note timestamp is a reserved word, you should be careful when using such field names. If you have to use a reserved word as field name, you must enclose it with backticks.
maybe a for could work
$i=1
for ($row='';$count=0;$i++) {
$result = mysql_query("SELECT link,title,timestamp,photo,link,author,comments FROM posts WHERE timestamp='*Todays Date -$i *' LIMIT 6");
$count = mysql_num_rows($result);
}
I think the query that you want both filters the results and orders them.
SELECT link, title, timestamp, photo, link, author, comments
FROM posts
WHERE timestamp <= '*Todays Date -1*'
ORDER BY timestamp desc
LIMIT 6;
If you actually want 3 rows and not 6 (as suggested by the post title), then change the 6 to 3.
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