wildcard for number in MySQL Query? - mysql

I have following rows in my database table,
number:
1
2
3
4
100
101
102
103
104
200
201
202
I want to get output of all 100's like[100,101,102,103,104], but i have tried following codes,
SELECT * FROM table WHERE number RLIKE '[[:<:]]1';
it shows [1,100,101,102,103,104]
SELECT * FROM table WHERE number LIKE '1%';
it shows [1,100,101,102,103,104], i only want get number from 100 to 104, i don't want number 1.

to get all numbers between 100 and 199 you can do this:
SELECT * FROM table WHERE number BETWEEN 100 AND 199;

You have to use between clause like following which select range.
SELECT * FROM table WHERE number BETWEEN 100 AND 199;
OR
SELECT *
FROM test
WHERE number LIKE "1%"
AND length(number)=3;
SQL Fiddle

Related

Get the Top Four Value from data table . Then swap on condition

using this command i can get TOP four values but some of the values are duplicate . so then wanted to swap based on serial number of table . using this bellow command to get the top four values now i want to swap any logic.
INPUT
SN Set name columname
1 100 Randy 25
2 100 many 22
3 100 sanny 22
4 100 nanny 35
Output
SN Set name columname
2 100 many 22
3 100 sanny 22
1 100 Randy 25
4 100 nanny 35
select top 4 * from filename where Set=100 order by columname DESC
Sort it based on column name then swap it based on serial number.
Wrap your query returning the wanted 4 rows up in a derived table, then do the opposite ORDER BY to get the wanted order:
select *
from
(
select top 4 * from filename where Set=100 order by columname DESC
) dt
order by columname ASC

Fetching 1000 + records quikly

Am using java as language,Spring Mvc and Hibernate as Frameworks,mysql as database.
I have 1000 records to show.its taking long time to fetch 1000+ records in single request.so ,i want to fetch 25 records first ,then next 25 records,then next 25 records like that.how to do that?
Use Query with limit option, with pagination concept
for example
select * from table limit 0,25
second 25 result
select * from table limit 25,25
every request calculate (page number * 25);
if page is 20 then 20*25
select * from table limit 500,25
Its working Fine with my little change
For First 25 Result
select * from table limit 0,25
For this query i will Get 1 to 25 Records
second 25 Result
select * from table limit 25,25
For this query i will Get 26 to 50 Records
next 25 Result
for Example if page is 3
as per your calculation (every request calculate (page number * 25);)
(3*25)=75
select * from table limit 75,25
For this query i will Get 75 to 100 Records
But Actucally i need 3rd 25 Records which is 51 to 75
so I Changed calculation like this
every request calculate (page number -1 * 25)
if page is 3 then ((3-1) *25) =(2*25)=50
select * from table limit 50,25
For this query i will Get 51 to 75 Records .which is i Wanted......

MySQL Trouble with a query

I have a table that for example it contain 1000 records. The query that I'm trying to do, is for get some like this:
substring_part_name number_of_warehose number_of_parts
156 1 50
156 2 140
156 3 300
180 3 130
120 1 80
120 2 300
And so obtain the 1000 records.
The trouble is this, the part_name is something like this: x_156, b_156, d_156, h_120, f_120 and so on. Every part has its corresponding warehouse.
The first column i get it on this way: distinct(substring(part_name,3)) as substring_part_name, I only want the last part of the name, How i can obtain that result??
My query is this:
select distinct(substring(part_name, 3)) as substring_part_name, count(#the number of parts by ware_house), ware_house from ware_houses
group by substring_part_name;
Use a negative integer for the SUBSTRING (-3) to get the last three characters.
select
distinct(substring(part_name, -3)) as substring_part_name,
number_of_warehouse,
number_of_parts
from table
You can also use RIGHT:
distinct(right(part_name, 3)) as substring_part_name

Get IDs based corresponding value pairs uniquely

Think of a table like this,
ID Value
100 1
100 2
101 1
101 2
102 1
103 2
104 1
Now i want to retrieve the IDs based on their corresponding value combination uniquely. (ie) 100 is having both 1 and 2 as values. 101 is also having 1 and 2. Now i don't want both 100 and 101. i want only one of them (either 100 or 101). similarly, i don't want both 102 and 104 since they both have values 1. i want only of them. Typically, my result should be as follows
ID
100
102
103
I want the IDs with corresponding value pairs uniquely.
i am using MYSQL. please help me write the query to find this.
You could try something like this:
select
min(id)
from (
select
id,
group_concat(value order by value) as values
from <table>
group by id
) r
group by values
How this works:
the subquery returns one row for each id, with an extra column that has all of the values (in sorted order, which is important for the next step)
the outer query selects the minimum id for each set of values

Perform sum, average, and multiplication across all columns in a table?

I have a table that looks like this:
id test1 test2 test3 test4 ... test200
1 90 87 85 86 70
2 100 95 83 92 80
.
.
18000
I know there are standard operations to perform sums and averages on a single column and multiply the value of two columns together but is it possible to do it across all columns in a row with a given id? If its not clear, I want to do something like this across rows instead of across columns. Thanks
You might be better off redesigning the table so that it does not have 200 columns.
e.g.
Id testnum score
1 1 90
1 2 87
...
2 1 100
2 2 95
...
180000
Now you can do a query like this:
select sum(score) as totalscore
from mynewtable
where id=1
How about:
select id, sum(test1 + test2 + ...) as summation
group by id
Is the problem that you have so many columns? This solution doesn't handle many columns smoothly.
There might be some contorted SQL to work on the contents of the whole row (I doubt that), but you still have to specify the column names, otherwise you'd have the ID numbers included in the calculation.