Mind burning query for me.
I want to fetch the all record between two age condition. Any one please help me. My query is write below
SELECT tbl_trip.* FROM tbl_trip WHERE ((tbl_trip.minage >= '15' AND tbl_trip.maxage <= '15') OR (tbl_trip.maxage <= '28' AND tbl_trip.minage >= '28'))
In this query i want all record from database .. where age lie between 15 to 28 . In this all reords get, e.g. minage to maxage : 1 to 16,12 to 30 , 16 to 24, 27 to 28 But not get like 3 to 13 or 29 or 100 . Thanks in advance.
Here is the right way Rohit wrote a confusing Query.
select * from tbl_trip where ((minage>=12 and minage<=22) or (maxage>=12 and maxage<=22)) or ((minage<=12 and maxage>=22))
Thanks
As I understand, you need to get all the records from the DB where minage--maxage range intersects with the given one. Hereinafter the given range will be from X to Y, the row's range will be from A to B. Obviously, Y > X, B > A.
There are Three situations in which these ranges cross:
A--B is completely inside X--Y. That means that on the numeric line these numbers lie like this: A, X--Y, B. Therefore, the condition is
A <=X and B >= Y
A--B patially crosses the X--Y on the left side of X--Y. On the numeric line these numbers look like: A, X, B, Y. Therefore, the condition is
A <= X and X <= B and B <= Y
Same for A--B crossing the X--Y range on it's right side. Numeric line: X, A, Y, B. Condition:
X <= A and A <= Y and Y <= B
In conclusion, the final condition is:
(A <= X && B >= Y) || (A <= X && X <= B && B <= Y) || (X <= A && A <= Y && Y<= B)
Try with it, its working fine for me
select * from tbl_trip
Where GREATEST(GREATEST(minage,12)-LEAST(maxage,22),0)=0
This query checks your given range between minage and maxage, and vice versa.
select * from tbl_trip
where (((minage>=15 and minage<=22) or (maxage>=15 and maxage<=22))
or((15>=minage and 15<=maxage) or (22<=maxage and 22>=minage)))
This is also working, Please check it properly before you use:
select * from tbl_trip
where ((minage>=12 and minage<=22) or (maxage>=12 and maxage<=22))
or ((12>=minage and 22<=maxage))
Here is another way:
select * from tbl_trip
Where GREATEST(GREATEST(minage,12)-LEAST(maxage,22),0)=0
Related
I am preparing for my Database final and I would like to understand these two questions. Could you please explain to me which is the correct answer and why it is correct.
Suppose that you have a relation with the schema R(X, Y, Z). Every value of X is unique, but the
other columns could have duplicate values. Assume that a sparse index is created for relation R on attribute X. Which of the following queries would use this index effectively?
(a) SELECT MAX(X)
FROM R
(b) SELECT MAX(Y)
FROM R
GROUP BY X
(c) SELECT *
FROM R
WHERE X <> 30
(d) SELECT MAX(Y)
FROM R
WHERE X = 23
(e) none of the above uses the index effectively
I believe (a) could be the correct answer as we have an index for X and they are all unique values.
Suppose that you have a relation with the schema R(X, Y, Z). Every value of X is unique, but the
other columns could have duplicate values. Assume that a dense index is created for relation R on attributes X and Y. Which of the following queries would use this index effectively?
(a) SELECT *
FROM R
WHERE X < Y
(b) SELECT DISTINCT X, Y
FROM R
WHERE X = 23 AND Y > 39
(c) SELECT X, Y
FROM R
(d) SELECT X
FROM R
WHERE Y = 23
(e) none of the above uses the index effectively
I believe (c) could be the correct answer as we have an indexes for both X and Y.
MySQL does not have "dense" vs "sparse". Here are the optimal indexes:
(a) SELECT MAX(X) FROM R -- INDEX(X)
(b) SELECT MAX(Y) FROM R GROUP BY X -- INDEX(x,y); INDEX(x) is not as good
(c) SELECT * FROM R WHERE X <> 30 -- No index is _likely_ to be useful
(d) SELECT MAX(Y) FROM R WHERE X = 23 -- Does not make sense if X is unique
(e) none of the above uses the index effectively -- Some: a,b,d
and
(a) SELECT * FROM R WHERE X < Y -- no index
(b) SELECT DISTINCT X, Y FROM R WHERE X = 23 AND Y > 39 -- Dumb due to uniqueness
(c) SELECT X, Y FROM R -- no index; just read the table
(d) SELECT X FROM R WHERE Y = 23 -- INDEX(Y,X), or, not as good, INDEX(Y)
(e) none of the above uses the index effectively -- ambiguous; note that (Y,X) is not same as (X,Y)
There's two table in my database:
Table A
Column_A1 column_A2
A1 10
A2 20
A3 30
Table B
Column_B1 column_B2
B1 11
B2 21
B3 31
B4 29
B5 30.5
I want to calculate how many row of table B match the following condition:
range:
A1±1,
A2±1,
A3±1,
...
for example:
B1∈[A1-1,A1+1]
count these row, return value 1.
B2∈[A2-1,A2+1]
count these row, return value 1.
B3∈[A3-1,A3+1]
B4∈[A3-1,A3+1]
B5∈[A3-1,A3+1]
count these row, return value 3.
Result should be like this:
Column_A1 column_A2 num_match
A1 10 1
A2 20 1
A3 30 3
It's easy to use a loop to do this in other programming language, but what's the simplest way to make it in SQL ? Thanks.
I would do this with a correlated subquery:
select a.*,
(select count(*)
from b
where b.column_b2 between a.column_a2 - 1 and a.column_a2 + 1
) as num_match
from a;
Note: between is used suggesting that the bounds are included in the range. If this is not the intention, then use explicit < and > logic.
Many databases would be able to take advantage of an index on b(column_b2) for this query. You can test on MySQL to see if this is the case.
You can use a GROUP BY statement and filter on inequalities:
SELECT Column_A1,Column_A2,COUNT(*)
FROM A JOIN B ON column_A2-1 <= column_B2 AND column_B2 <= column_A2+1
GROUP BY Column_A1,Column_A2
A simple query that matches with the way the OP expressed the goal of the statement:
SELECT
a.`Column_A1`,
COUNT(*) as `NumMatch`
FROM `Table_A` a
JOIN `Table_B` b
ON b.`column_b2` BETWEEN a.`column_A2` - 1 AND a.`column_A2` + 1
GROUP BY a.`Column_A1`;
My problem is how to loop through a table and extract information from another table.
I have a table - X with 470 records:
A B C
111 12 18
121 21 29
127 37 101
I would like to write the following query:
create or replace view NEW as
For j = 1-3
Select * from Y
where imei = X.A(j) and id > X.B(j) and id < X.C(j)
Apologies, I am a matlab programmer so I have used that syntax above to explain what I want. How can I do this in MySql? I have looked up For Loops but mostly it loops through within the same table. I need to loop through a different table and use those criteria in the where statement of a different table.
To get 3 rows from a table, use LIMIT 3 in a subquery. To get related rows in another table, use JOIN.
CREATE OR REPLACE VIEW new AS
SELECT Y.*
FROM Y
JOIN (SELECT *
FROM X
LIMIT 3) AS X ON Y.ime1 = X.a AND Y.id > X.b AND Y.id < X.c
To make LIMIT 3 produce predictable results, you should have an ORDER BY clause in the subquery. Otherwise, it will select an arbitrary set of 3 rows from X.
i need to run query which do the following:
order the table by x , so the result will be.
then it do a multiple order by for a groups according to the following x range.
so userId_1 will be before userId_2 , because both are in 90-100 x range.
and userId_1 y value > userId_2 y value.
How to implement that ? thanks
You should be able to get the desired effect by dropping the last digit of x for ordering, i.e.
SELECT *
FROM MyTable
ORDER BY x DIV 10, y
I have a database column like this:
id
50
55
56
62
63
64
65
68
70
72
80
etc...
I want to iterate through the id column with the following formula to find if the result of the formula is an id number in the same column. I want to compute all the possible combinations of the set of basically 3 records in the id column.
First loop:
Does ((second_id_number - first_id_number) * variable decimal) + second_id_number equal a number in the id column?
Per the formula, the first loop is
(55-50)*2.00(as an example of variable decimal) + 55 = 65. 65 is in the list => 65 is tagged with the 2 records which equal it
Second loop:
Does ((third_id_number - first_id_number) * variable decimal) + second_id_number equal a number in the id column?
(56-50)*2.00(as an example of variable decimal) + 56 = 78. 78 is not in the list => 78 is not tagged
Third loop:
Does ((fourth_id_number - first_id_number) * variable decimal) + second_id_number equal a number in the id column?
etc...
I want the results to show all the tagged records. A tagged record is the set of the 3 records where the third record is the result from the formula.
Anyone got any ideas? Is it possible in mysql?
Thank you
If I'm understanding your requirements properly, it sounds like you'd want to use a self-join on the table, e.g.
SELECT ...
FROM yourtable AS parent
LEFT JOIN yourtable AS child ON
FLOOR((parent.second_id_number - parent.first_id_number) * variable) + parent.second_id) = child.id
You could potentially carry something like this forward, which satisfies your first "loop"
select a.id as first_id_number
, b.id as second_id_number
, ((b.id - a.id) * 2) + b.id as third_id_number
from my_table as a
join my_table as b on a.id = (select max(id) from my_table where id < b.id)
where ((b.id - a.id) * 2) + b.id in (select id from my_table)
According to your description and test data, this would show 65 as "tagged" with first_id_number 50 and 62.
Warning: done on SQL Server using what I think is fairly standard syntax. I would understand if some would rather phrase this as a cross join with the select max... bit in the where clause rather than in the join predicate.