Valid or Invalid state in mySql [closed] - php

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;

Related

group by hour and post (multiple) [closed]

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

Mysql and PHP select Join Count [closed]

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.

How to retrive data from SQL from specific row to last record [closed]

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 get data from database from specific row to last row.
How can I do it?
You can set limit and offset with your sql query like this.
SELECT *
FROM your_table
ORDER BY id DESC
LIMIT 1, 1;
See more : http://www.mysqltutorial.org/mysql-limit.aspx
$sql="Select * from table order by id desc limit 20";//It will give you last 20 records

Select statement with sum and limit [closed]

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 8 years ago.
Improve this question
What's wrong with this query and how can i output the result?
`SELECT sum(BM) from (SELECT rpdc.BM from rpdc LIMIT $start, $limit) as totalbm`
You need to give an alias for the sum something as
SELECT
sum(BM) as total_sum
from (SELECT rpdc.BM from rpdc LIMIT $start, $limit) as totalbm
Then in php you can use as
$row['total_sum'];
Hey you need to add as 'some_name' like SELECT name as firstname
Replcae with this sum(BM) as total
So, result array will treat total_sum as key of array.
You can use it as $result['total']

MySQL How to print the result of this query? [closed]

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 8 years ago.
Improve this question
I have this query:
$sql = $link->query("SELECT m.id_rel, (SELECT SUM(visita) as total FROM icar_mas_vistas WHERE id_rel = m.id_rel), icar_categorias.nombre
FROM icar_mas_vistas AS m, icar_categorias WHERE m.id_rel = icar_categorias.id_categoria");
if($sql->num_rows){
while($row = $sql->fetch_object()){
echo ''.$row->total.' '.$row->nombre.'';
}
It's working great in the SQL console, but when printing in PHP the value of total is not showing, how can i print it?
Thanks
Try change query adding AS total after brackets, to:
SELECT m.id_rel,
(SELECT SUM(visita) as total FROM icar_mas_vistas WHERE id_rel = m.id_rel) AS total, icar_categorias.nombre
FROM icar_mas_vistas AS m, icar_categorias
WHERE m.id_rel = icar_categorias.id_categoria

Categories