select all rows when its values smaller than specify number? - mysql

i have a table :
a b c
1 10 1001
7 6 54
56 2000 31
1200 5 9
4 10 20
2 65 20
how can i select rows with column's value of this row smaller than 1000. i want to get this
a b c
7 6 54
4 10 20
2 65 20
mysql query still get all value :
SELECT a,b,c FROM test
where a <'1000' or b<'1000' or c<'1000'

It sounds like you would like to pull a row where there is NO column greater than 1000 in that row, if that is correct then you need to us AND instead of OR.
SELECT a,b,c FROM test
where a <'1000' AND b<'1000' AND c<'1000'
Hope that helps!

Related

MySQL: Get count for each range

There is mysql Ver 8.0.18 value_table as:
value count
1 3
11 1
12 2
22 5
31 1
34 3
35 1
40 3
46 7
What is query to get a total count for each dozen (1-10 - first dozen,11-20 - second , etc..)
as:
1 3
2 3
3 5
4 8
5 7
Query should be flexible, so when some records added to value_table , for example
51 2
62 3
so, it is not necessary to change a query by adding new range (51-60 - 6-th dozen, etc.)
I think you just want division and aggregation:
select min(value), sum(count)
from t
group by floor(value / 10);
To be honest, I'm not sure if the first column should be min(value) or floor(value / 10) + 1.

Referencing values in a different column with matching name in MySQL

Getting stumped. How would I create a new column items_delta. I want items_delta to be a value that uses the column name, items, and last date. I need to find the delta between two different dates for the same name.
Ex: For name = A, the latest date is 12/14, and the items = 150. For name = A, I want the difference in items now compared to the items from the most previous date only. So essentially for name = A, I want (150 - 120)/120 = 0.25 which is the difference from the two latest dates.
Test Table
id name items last_date
1 A 150 12/14/15
2 B 100 12/14/15
3 C 50 12/14/15
4 A 120 12/13/15
5 B 110 12/13/15
6 B 90 12/12/15
7 A 110 12/12/15
8 A 100 12/11/15
9 A 150 12/10/15
10 C 35 12/10/15
What I want:
Test Table 2
id name items last_date items_delta
1 A 150 12/14/15 0.25
2 B 100 12/14/15 -0.09
3 C 50 12/14/15 0.43
Relatively new to mySQL, please be gentle.

sql query to extract rows

table:
id user_id guessed_id result
1219 27 4 Y
1357 20 4 Y
2 3 5 N
9 20 5 N
1392 20 11 Y
1618 27 11 N
2471 20 25 Y
I would like to build a query that fetches only the guessed_ids that have exactly 2 result with the value Y.
In the above rows the query should return:
4
since the rows containing the guessed_id 4 have both Y in the column result.
guessed_id 5 should not be returned since it contains 2 N
11 should not be returned since it has a N
25 should not be returned since it has only one Y
I am using mysql DBMS.
A GROUP BY/HAVING will do what you want, just count the number of Y's and return the guessed_ids where the count is 2;
SELECT guessed_id
FROM myTable
WHERE result='Y'
GROUP BY guessed_id
HAVING COUNT(*)=2
An SQLfiddle to test with.
I'm not sure from your example whether any N results are allowed for a hit, I assumed they're not relevant as long as there are exactly 2 rows with a Y.

Mysql :Exclude row that does not satisfy the condition list

So Here is My data
ID C1 C2 C3
6 Digit 2 6,8,10,12
12 Digit 3 15
15 127 Digit 2 6,7,8,9,10,11,12,13
68 140,141 Digit 11 85,86,87,88,167,168,158,159
73 1 Digit 11 85,86,87,88,169,170
76 Digit 11 85,86,87,91,164,165,166,167,168
99 Digit 11 20,27,85,86,87
106 Digit 1 1,2
111 Digit 11 85,86,87,88
112 Digit 11 85,86,87,88
135 Digit 11 85,86,87
and my condition string is (2,6,15,37,42,52,62,65,79,85,94,100,104,107,113,124,131)
Now,I want to exclude row 3,4,5 if the values 127,140,141,1 are not in the list condition. I tried Not in , but no avail. I think I might be missing something basic, but just cant get it.
It's better not to store multiple values in a column if possible. Then it's easier to do queries like this.
You cannot use "IN" or "NOT IN" because they are looking for a list of separate items. But C3 is just one item that happens to have commas in it.
Try this:
SELECT * FROM
(SELECT ID, C1, C2, CONCAT('|',REPLACE(C3,',','|'),'|') as C3 FROM `table` WHERE `C3` ) as t1
WHERE t1.C3 NOT LIKE "|127|" AND t1.C3 NOT LIKE "|140|" AND t1.C3 NOT LIKE "|141|" AND t1.C3 NOT LIKE "|1|"
You could avoid the "|" and just concat "," to the start and end.
Or you could fix your database schema so that it actually acts like a Normalized Relational Database.
Every column that contains multiple values should be separated out into its own table.
There should be no column C3 in your table above. Instead, you should have a table, some_other_data:
At this point, I see that C3=6 is related to more than one record in the main table. Therefore, you actually need a third, linking table, in addition to some_other_data. See below.
`some_other_data`
id
6
8
10
12
15
`main_table_to_some_other_data_link`
some_other_data_id | main_table_id
6 6
8 6
10 6
12 6
15 12
6 15
etc. You can see that the linking table can contain duplicates of either value. But your other two tables would have completely unique ids.
I think you're trying to solve the wrong problem.
(I'm assuming you can change your table structure. If you can't someone else will need to address your question.)
The long lists of comma-separated data are a flag that they have a one-to-many relationship with ID.
For example, make the data in C3 its own table:
ID MainID C3
================
1 6 6
2 6 8
3 6 10
4 6 12
5 12 15
6 15 6
7 15 7
8 15 8
9 15 9
10 15 10
11 15 11
12 15 12
13 15 13
// and so forth //
So ID is the primary key of the new table, MainID is the foreign key that refers to the record in your primary table, and C3 is the data in C3.
Each separate value of C3 now has its own record.
Now, you're in a position to use something like
Select * from MainTable
Inner Join NewTable
On MainTable.ID = NewTable.MainID
Where NewTable.C3 Not In (2,6,15,37,42,52,62,65,79,85,94,100,104,107,113,124,131);
If you can, pulling out the one-to-many relationships into their own tables will make things easier for you.

Querying a table to get values based on no of digits of a parameter?

Considering the following table
I have a large table from which I can query to get the following table
type no of times type occurs
101 450
102 562
103 245
111 25
112 28
113 21
Now suppose I wanted to get a table which shows me the sum of no of times type occurs
for type starting with 1 then starting with 10,11,12,13.......19 then starting with 2, 20,21, 22, 23...29 and so on.
Something like this
1 1331 10 1257
11 74
12 ..
13 ..
.. ..
2 ... 20 ..
21 ..
Hope I am clear
Thanks
You really have two different queries:
SELECT [type]\100 AS TypePart, Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100;
And:
SELECT [type]\100 AS TypePart, [type] Mod 100 AS TypeEnd,
Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100, [type] Mod 100;
Where t is the name of the table.
Here on the first query i am getting something like this
utypPart CountOftype
1 29
2 42
3 46
4 50
5 26
6 45
7 33
9 1
it is giving me how many utyp are starting with 1 2 and so on
but whai i want is the sum of no of times those types occur for the utyp .