Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have already searched for possible answers but did not find any who helped me further.
I have two tables. One named users, where the user details are saved like: Name, ID, etc.
Second one is a table where the payments are stored.
Payments:
payment_id
date
amount
user_id
completed
Now I try to get all the payments of a user:
SELECT COUNT(amount) FROM ...
I already have tried this but did'nt gave me the right amount that has been paid.
SELECT
g.name,
g.user_id,
COUNT(m.amount) AS totalAmount
FROM users AS g
LEFT JOIN payments AS m ON g.user_id = m.user_id
GROUP BY g.user_id
Do you just want... SUM(m.amount) instead of COUNT(m.amount) ?
COUNT(m.amount) gives you the number of rows that were aggregated (that means the total number of payments).
SUM(m.amount) is the cumulated value of field m.amount, that is the total amount.
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 2 years ago.
Improve this question
I want something like this:
Table
And my database table is like this:
Database
How can I get this records group by (hour and post) at the same time?
And How can I print this results on html?
SELECT post_id, hour, COUNT(*) FROM <table_name> GROUP BY post_id, hour
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am trying to use code from the following old question:
MySQL: Count occurrences of distinct values
My query is as follows:
$result = $db->query("SELECT name,COUNT(*) as cnt FROM `table` GROUP BY name ORDER BY cnt DESC");
$row = mysqli_fetch_array($result);
var_dump($row);
In phpmyadmin this code will output all the name and COUNT columns. When I run var_dump($row) it will only have one row for me to work with (the first one, ie the one with the most occurrences), which I can't figure out why. Any ideas? Thanks.
From the documentation on mysqli_fetch_array:
Fetch a result row as an associative, a numeric array, or both
As you found out, it does just that: it fetches a row.
Maybe you were expecting the behaviour of mysqli_fetch_all?
Fetches all result rows as an associative array, a numeric array, or both
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.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am using Yii, I want to select all records of a model except the last 2 records, but I got an error that there is syntax error, here is my code:
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents) FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents))";
$model2 = Newsevents::model()->findAll($c);
This one considers, that idNewsEvents might not be sequential (due to deletes or whatever).
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents NOT IN (SELECT idNewsEvents FROM newsevents ORDER BY idNewsEvents DESC LIMIT 2)";
$model2 = Newsevents::model()->findAll($c);
P.S.: Not sure if this is correct yii syntax, I'm not familiar with it, but I'm very familiar with MySQL. Anyway, you get the idea I hope.
Two checking can be done in a single checking
$c->condition ="idNewsEvents < (SELECT MAX(idNewsEvents)-1 FROM newsevents)";
Example query
SELECT * FROM `users` WHERE user_id < (SELECT MAX(user_id)-1 FROM users)
it should be:
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents)
FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents)";
There were just an extra bracket at end and a very tired eyes to notice that :( .
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