Trying to SELECT everything but excluding between IDs doesnt work - mysql

I'm trying to SELECT everything but not between the 6 id and 12 id but it doesn't work it only returns everything
Here is my code:
SELECT nom FROM genre WHERE id_genre BETWEEN id_genre=!6 AND id_genre=!12;
Thanks.

When you write condtition WHERE id BETWEEN 6 AND 12, what you want is to find those ids that are greater or equal than 6 and less or equal than 12, i.e. id >= 6 AND id <= 12.
Now, you want to negate it, so you'd write (accordingly to mathematical logic rules): id < 6 OR id > 12, which means you want ids outside this range.
Or, you could write simply: NOT (id BETWEEN 6 AND 12).

Related

Mysql-query - find all rows where ID is divisible by 4

Is it possible to create a query that returns all rows where ID (auto increment integer) is divisible by 4?
select * from mydatabase where id is divisble by 4 oder by id asc.
for instance i would like to get the rows with id 4, 8, 12, and so on..
Edit:
just as additional information for completeness, what if I would need the rows 1, 5, 9 etc?
here % do if your id divide by 4 and it give without reminder means equal to 0 then it will give you a result.
SELECT * FROM table WHERE (id % 4) = 0;
if you need 1,5,9 then you need to do id-1%4
it is means you need 4+1=5,8+1=9 so you need to do ((id-1)%4
remember do not forget to put (id-1) like this. because without it it will follow math rule and do first 1%4
SELECT * FROM user_rules where ((id-1) % 4)=0 limit 0,4
You can also use the bitwise operator & because 4 is a power of 2.
You can replace % 4 with & (4 - 1) making it & 3
Using in a query.
Query
SELECT
*
FROM
[table]
WHERE
(id & 3) = 0
This seems already too easy...
SELECT *
FROM table
WHERE id % 4 = 0

mysql select max percent id and all row. or select latest insert

i have this table called "tblpercentage"
PercentID Dividend Rebates Interest
1 40 10 2
2 60 20 5
this is my query
SELECT MAX(PercentID)As PercentID,Interest FROM tblpercentage
but the problem is. it can only select PercentID = 2 and interest = 2 instead if 5. but i want is select all row with the max id. can anyone help me.
for get the row related to the max id you should use a subquery
select PercentID,Interest from tblpercentage
where PercentID = (SELECT MAX(PercentID) from tblpercentage)

How do I make a google query that selects for row?

Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.

How to find out next available number

In my MySQL table I have field called sequence where I have values like
1 , 2, 3, 5, 6, 7, 8, 10 some of the sequence number are skiped due to deleted records. How do I find out next available number from given number. let's say if I need next number from 3 , how do I get number 5 as my next number in sequence not the 4.
To find out the next ID after 3 that appears in your table, you should do
SELECT id FROM thetable WHERE id>3 ORDER BY id ASC LIMIT 1
This just considers IDs that are greater than 3, in ascending order, and then takes the first one on that list. If it returns you one result, then that's the next one used in the table; if it doesn't return a result at all, then the ID you gave it was already the highest one in the table (or, strictly speaking, at least as high as the highest one in the table).
If you want a general expression that works to get the next available number, then you can use an aggregation query:
select coalesce(max(id), maxid + 1) as NextAvailableId
from table t cross join
(select max(id) as maxid from table t) x
where id > 3;
Or, if you don't like the cross join, you can use conditional aggregation:
select coalesce(max(case when id > 3 then id end), max(id)) as NextAvailableId
from table t;

Retrieve maximum value from a table containing duplicate values according to a condition

I have a table tbl_usertests from which i want to retrieve the user who have maximum testscore for each test.
Note: User here means usertestid which is unique.
Its colums are:
pk_usertestid attemptdate uploaddate fk_tbl_tests_testid fk_tbl_users_userid testscore totalquestionsnotattempted totalquestionscorrect totalquestionsincorrect totalquestions timetaken iscurrent
data :
1;NULL;"2010-06-24 22:48:07";"11";"3";"1";"53";"1";"21";"75";"92";"1"
2;NULL;"2010-06-25 01:21:37";"11";"4";"13";"0";"13";"62";"75";"801";"1"
3;NULL;"2010-06-25 01:21:50";"10";"4";"17";"5";"17";"53";"75";"640";"1"
4;NULL;"2010-06-25 01:24:23";"11";"4";"13";"0";"13";"62";"75";"801";"1"
5;NULL;"2010-06-25 01:24:47";"10";"4";"17";"5";"17";"53";"75";"640";"1"
6;NULL;"2010-06-25 01:36:04";"11";"5";"13";"0";"13";"62";"75";"801";"1"
7;NULL;"2010-06-25 01:47:26";"7";"5";"10";"1";"10";"49";"60";"302";"1"
My Query is :
SELECT max(`testscore`) , `fk_tbl_tests_testid` , `fk_tbl_users_userid` , `pk_usertestid`
FROM `tbl_usertests`
GROUP BY `fk_tbl_tests_testid`
This query output:
max(`testscore`) fk_tbl_tests_testid fk_tbl_users_userid pk_usertestid
10 7 5 7
17 10 4 3
13 11 3 1
But the problem is that if there are two users who have same score, it displays only one user because i have used group by clause.
For. e.g. testid =10 i have two records(pk_usertestid 3 and 5) but it displays 3 only.
I want the user whose upload date is less than the other user(in case of two users having same testscore). It should display for usertestid=3 since 3 upload date is less than 5.
Right now its displaying 3 but it is due to group by clause.
I am unable to construct the query.
Please help me on this
Thanks
Try this:
SELECT t.`fk_tbl_tests_testid` , t.`fk_tbl_users_userid` , t.`pk_usertestid`, maxscores.maxscore
FROM `tbl_usertests` t
JOIN (SELECT `fk_tbl_tests_testid`,max(`testscore`) as maxscore
FROM `tbl_usertests`
GROUP BY `fk_tbl_tests_testid`) maxscores ON t.`fk_tbl_tests_testid` = maxscores.`fk_tbl_tests_testid`
the logic behind is to separate the whole thing into two parts: get the maximum (or any other aggregate) values for each group (this is the subquery part), then for each element, join the corresponding aggregate. (JOIN it back to the riginal table)