I am a beginner in MySQL and i want to do a check for values but within a range, i have this:
SELECT t1.width, COUNT( t1.width )
FROM test t1
INNER JOIN (
SELECT t2.width
FROM test t2
GROUP BY width
HAVING COUNT( t2.width ) >1
)t2 ON t1.width BETWEEN (t2.width +1000) AND (t2.width -1000)
ORDER BY t1.width
So what i want to do is to check if there is two values of 'width' with a difference of +1000 or -1000.
The result is always null.
could you please tell me what is wrong with the query?
I don't fully understand what your data is. The way I understand is you are looking to see if two values from two columns have a specific difference, i.e the first value in the first column is 2000 and the first value in the second column is 1000, since there is a difference of 1000 you want this noted. You could use the CASE function (more detail here https://www.w3schools.com/sql/func_mysql_case.asp).
Say you have one column called width_1 which consists of different values of widths, and a second column called width_2 which also consists of different width values, all contain in a table called width_table, you could use the following:
SELECT
CASE
WHEN width_1 - width_2 = 1000 OR width_1 - width_2 = -1000 THEN TRUE
ELSE FALSE
END AS column_name
FROM width_table ;
This will produce a column whose entries are either 1 if the difference is exactly +1000 or -1000, or 0 if the difference is anything else.
If you want to check if the difference is between 1000 and -1000, then you can use the following:
SELECT
CASE
WHEN width_1 - width_2 BETWEEN -1000 AND 1000 THEN TRUE
ELSE FALSE
END AS column_name
FROM width_table ;
Related
A table called test has columns :
person_id (Unique) & special_num
Table:
Outcome :
Would like to create a new column called div to determine which person got the special_num that is divisible by 10 and which person did not instead of true or false would like to have a yes or no.
I am new to MySQL and have never tried it but gave it a shot please tell me how to get this:
SELECT * FROM test WHERE special_num % 10 = 0 AS div from test;
I am unable to figure out how to input values and if it is the right way of doing it
If you want a column, then it goes in the select:
select t.*,
( (special_num % 10) = 0) as div
from t;
You need a CASE expression:
SELECT *,
CASE WHEN special_num % 10 = 0 THEN 'Yes' ELSE 'No' END AS `div`
FROM test
Here is my SQLFiddle to mimic this problem.
First step is to create a new column by ALTERing the table.
ALTER TABLE PERSONS
ADD is_special VARCHAR(3);
Now run an UPDATE query that checks if the special_num % 10 is zero or not, and if it is, then set is_special to yes.
UPDATE PERSONS SET is_special = CASE WHEN special_num % 10 = 0 THEN 'yes' ELSE 'no' END;
Having said that, it is a bad idea to store values that are derived or calculated from other fields. You may want to use this in a view, but not in a table.
I have the following query :
SELECT SUM(P_QTY)
FROM rankhistory
WHERE P_ID= '1'
AND RH_DATE>=1438556400
AND RH_DATE<1438642800
The above query returns 268
The result set contains two elements of P_QTY which are 160 and 108
Now what I want is be able to receive the difference instead of the sum, so what I want my query to return is 52, how can I achieve that through sql query?
Please note that the subquery can return more than one result, and the intended is get the total change. For example if query returns 168 160 150, the result should be 18.
There's no aggregate function for difference, but since you know exactly which rows to use, you can select each value as its own subquery, then subtract the two columns from one another in a single select statement.
SELECT a.op, b.op, a.op - b.op as diff
FROM (SELECT 10 as op) a
JOIN (SELECT 8 as op) b
Expressed in accordance with your schema, it would probably look like this:
SELECT a.op, b.op, a.op - b.op as diff
FROM (SELECT P_QTY as op FROM rankhistory WHERE P_QTY = 160) a
JOIN (SELECT P_QTY as op FROM rankhistory WHERE P_QTY = 108) b
To use this approach regularly in an application, however, you'll want to handle it based on ID's or something else easily selectable and meaningful.
It sounds like you want something else, though. Perhaps you're interested in the difference between max and min during a date range?
SELECT MAX(P_QTY) - MIN(P_QTY) as diff
FROM rankhistory
WHERE rh_date BETWEEN '1438556400' AND '1438642800'
Two tables:
prefix ( id, value )
---------------------
1 'hello'
2 'good afternoon'
3 'good night'
suffix ( id, value )
---------------------
1 'world'
3 'world'
I'd like to get
all from table prefix which can be joined on table suffix via id
result should look like:
prefix.id prefix.value
--------------------------
1 'hello'
3 'good night'
well - quite easy so far...
but if table suffix is empty I'd like everything from table prefix
without subselects/ctes or if.... and in one query fulfilling both conditions!
Is there any trick to get this done by some magic having-clause or tricky something else?
Just for testcases: SQL-fiddle
Well, there is a way, but I agree with others that your requirements make no (practical) sense.
Anyway, here you go:
Join the suffix table twice (each time with a left join). One join is on the id column, the other on an always true condition.
Group the results on the prefix columns you want in the output and at least one non-nullable column of the first instance of suffix.
In the HAVING clause, put a condition that the first suffix column is not null or the number of values of a non-nullable column in the second suffix instance is 0. (Obviously, every group will have the same number of rows, i.e. the count will be the same for every prefix row.)
This is the query:
SELECT prefix.id, prefix.value
FROM prefix
LEFT JOIN suffix ON prefix.id = suffix.id
LEFT JOIN suffix AS test ON 1=1
GROUP BY prefix.id, prefix.value, suffix.id
HAVING suffix.id IS NOT NULL OR COUNT(test.id) = 0;
And there's also a demo at SQL Fiddle.
You need an OR and NOT EXISTS:
SELECT
prefix.id, prefix.value
FROM
prefix
WHERE
EXISTS(SELECT 1 from suffix WHERE prefix.id=suffix.id)
OR NOT EXISTS(SELECT 1 FROM suffix)
Demo
I guess the answer is: no, you can't!
Or if you can: No, you shouldn't.
Are there any solutions on MySQL script to filter the results with specific interval number.
For example, if I have 100,000 records in database and I'd like to get only the record number 1000, 2000, 3000, etc. (step by 1000).
I could do this on server side script by getting the entire results (e.g. 100,000) and use syntax like:
for($i=0, $i <= 100,000, $i = $i+1000) $filterResult[] = $record[$i];
However, as you may see, it would pull stress to the system as 100,000 records will need to generated first.
Are there any solutions that could complete from database script? Please note that, primary key may not start with 1 - 100,000 as the results based on some condition in where clause.
Your help would be really appreciated.
You can do:
SELECT *
FROM tbl
WHERE id % 1000 = 0
But it seems like you don't want to rely on the primary key value, but rather the row ranking of a result set.
In that case, you can do:
SELECT *
FROM (
SELECT *, #rn:=#rn+1 AS rank
FROM tbl
CROSS JOIN (SELECT #rn:=0) var_init
WHERE column1 = value AND
column2 = value
) a
WHERE a.rank % 1000 = 0
Where column1 = value AND column2 = value is just a placeholder for whatever filtration you're doing in your query.
I am trying to run a nested query but I am getting this error,
#1241 - Operand should contain 1 column(s)
this is the query that I am trying to run,
SELECT *
FROM `categoryTable`
WHERE `categoryId` NOT
IN (
SELECT `categoryTable`.`categoryId` , `categoryTable`.`categoryTitle` , `userMenuTable`.`menuEntryId`
FROM (
`categoryTable`
)
LEFT JOIN `userMenuTable` ON `categoryTable`.`categoryId` = `userMenuTable`.`categoryId`
WHERE `userMenuTable`.`cookieId` = 'bang4b696152b4869'
)
LIMIT 0 , 30
5th line should be
SELECT `categoryTable`.`categoryId`
i.e. it should only reference categoryId.
In other words, with the WHERE xyz [NOT] IN (SELECT ... predicate, there should only be one column in the nested select, one corresponding to the "xyz" column but of course not necessarily named the same. The reason is that SQL wouldn't know which column of the nested query to use for comparing with the "xyz" column; a lesser reason is that the other columns are useless, why bring them in?
Yes. I agree with #mjv, basically you are checking to see if categoryId is not in the list of
`SELECT `categoryTable`.`categoryId` , `categoryTable`.`categoryTitle` , `userMenuTable`.`menuEntryId`
FROM (
`categoryTable`
`
So you need to mention only one field categoryID and it will check to see if it is not in this list.
Hope this makes some sense.