Question

I'm trying to select rows where a certain column does not have a certain value, such as 0, but I'm unable to get this to work.

SELECT *
FROM rentals
GROUP BY date, rooms, price 
HAVING show_independently < 1

If show_independently is 1, then I don't want to group them. However, this statement shows no rows even though most rows have show_independently as 0.

Was it helpful?

Solution 2

If you only want to group some rows and leave others ungrouped, you can use a UNION:

SELECT *
FROM rentals
WHERE show_independently <> 1
GROUP BY date, rooms, price 
UNION ALL
SELECT *
FROM rentals
WHERE show_independently = 1

This groups only those where show_independently is not 1, and includes the rest without grouping them.

OTHER TIPS

SELECT date, rooms, price 
FROM rentals
WHERE show_independently < 1
GROUP BY date, rooms, price 

A HAVING clause is used when you are using an aggregate function to filter data.

A typical query with HAVING:

SELECT yourColumn, aggregate_function(otherColumn)
FROM yourTable
WHERE yourColumn = someValue
GROUP BY yourColumn
HAVING aggregate_function(otherColumn) = someOtherValue

I think you want to be using a WHERE clause:

SELECT date, rooms, price 
FROM rentals
WHERE show_independently < 1
GROUP BY date, rooms, price 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top