MySQL query with multiple BETWEEN is always NULL - mysql

Maybe I'm missing the obvious here, but why is this query always NULL?
SELECT ROUND(AVG(`viewer_count`),0) AS avg_viewer FROM `table`
WHERE
(id BETWEEN 1 AND 8)
AND
(id BETWEEN 26 AND 32)
Isn't it possible to get the average of multiple ranges like this?

Since it is not possible for an id to be between 1 and 8 and at the same time between 26 and 32, the query is returning NULL as no rows were matched. If you use OR as squemeamish suggested in the comment, you will get the average over both ranges.

SELECT ROUND(AVG(CASE WHEN id BETWEEN 1 AND 8 THEN `viewer_count` END),0) AS avg_viewer1_8
, ROUND(AVG(CASE WHEN id BETWEEN 26 AND 32 THEN `viewer_count` END),0) AS avg_viewer26_32
FROM `table`

Related

MySQL Query to replace string with value

I have requirement like as below.
Need a MYSQL query to replace value with maching the below condition.
i have a table containg the Product ID
Product_ID
1
2
3
4
5
15
25
I want to replace the 5 with value of 1.111. My requiremnet is this that it should only replace the 5 value not the 15 value.
example 5 should be 1.111 but it sould not replace the 15 value.
You can use IF() or CASE to select a different value when the value meets a condition.
SELECT IF(product_id = '5', '1.111', product_id)
FROM yourTable
or
SELECT CASE product_id
WHEN '5' THEN '1.111'
ELSE product_id
END
FROM yourTable
CASE generalizes more easily to other values that you want to replace, since you can have multiple WHEN clauses.

create columns and rows mysql

I have table data such as this
index user date rank
11 a 1Mar 23
12 b 1Mar 16
13 a 2Mar 24
14 b 2Mar 18
What I would like to achieve via a query is this:
1Mar 2Mar
a 23 24
b 16 18
I don't know if this can be done via a single statement at the command line or if this will have to be done via a form and some scripting. Doing through scripting I can do, but can't see how to do in a single statement.
you can do pivot like below, if you know all possible values for date
or you need to use dynamic sql.
SELECT user,
MAX( CASE WHEN date ='1Mar' THEN rank else NULL end) AS '1Mar',
MAX( CASE WHEN date ='2Mar' THEN rank else NULL end) AS '2Mar'
FROM Table1
GROUP BY user

Calculate total of some fields in mysql?

I have a table like this
item_id item_quantity
----------------------
324 2
432 23
543 12
879 3
The item_id field is not auto-increment but it is unique.
The inputs of the query will be some item_id values and the result should be sum of those in item_quantity.
For instance, if the inputs are 324, 543, then the query should return 14 (2 + 12)
Is it possible to do this at mysql level?
Try using in :
SELECT SUM(item_quantity)
FROM table
WHERE item_id in (324,543)
This works on MSSQL, not sure about MySQL, though:
SELECT SUM(item_quantity) FROM MyTable WHERE item_id in (324, 543)
EDIT: OK, others got the same answer. So I guess it also works on MySQL.
If all you need is the sum of the item_quantity, all you need to do is:
Select Sum(item_quantity)
From Table
Where item_id In (324, 543)

Unable to apply WHERE/AND on MySQL table with 2 columns on MAMP

I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)

Counting rows in mysql database

I want to count from the row with the least value to the row with a specific value.
For example,
Name / Point
--------------------
Pikachu / 7
Voltorb / 1
Abra / 4
Sunflora / 3
Squirtle / 8
Snorlax / 12
I want to count to the 7, so I get the returned result of '4' (counting the rows with values 1, 3, 4, 7)
I know I should use count() or mysql_num_rows() but I can't think of the specifics.
Thanks.
I think you want this :
select count(*) from mytable where Point<=7;
Count(*) counts all rows in a set.
If you're working with MySQL, then you could ORDER BY Point:
SELECT count(*) FROM table WHERE Point < 7 ORDER BY Point ASC
If you want to know all about ORDER BY, check out the w3schools page: http://www.w3schools.com/sql/sql_orderby.asp
Just in case you want to only count the rows based on the Point values:
SELECT count(*) FROM table WHERE Point < 7 GROUP BY Point
This may help you to get rows falling between range of values :
select count(*) from table where Point >= least_value and Point<= max_value