SQL plages de dates [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I ask myself why this request
SELECT * FROM world.city WHERE '20181109' > datedebut and '20181109' < datefin;
doesn't return the same result as this
SELECT * FROM world.city WHERE datedebut < 20181109 < datefin;
The last select sentence is not valid SQL, but mathematically it is the optimal solution

For each operator (ie < or > ) there must be a distinct right and left side.
When you write datedebut < 20181109 < datefin it is parsed as datedebut < 20181109 and the result of that comparison is sent to the next operator.
You need to think differently when writing SQL than when writing mathematics. You can argument that both "languages" have parsers, but they are slightly different.
EDIT comment:
As I have been made aware in the comments, My original answer was not entirely correct, since that would ave given a parse error. The essence is correct: Your query is not being parsed as you expected.
As others have answered, if you want a more compact answer, you need to use the BETWEEN keyword instead of the <and > operators.

There is a MySQL expression syntax which more closely matches your second form, which is BETWEEN:
SELECT * FROM world.city WHERE '20181109' BETWEEN datedebut AND datefin

Related

CASE clause inside WHERE clause [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
can anyone tell me why I can not run this code in SQL Management studio?
Thanks in advance.
SELECT * FROM TabKontaktJednani
where
TabKontaktJednani.Typ IN (
CASE 'ERP'--(SELECT ALIAS FROM TabCisZam where LoginId = SUSER_NAME())
WHEN 'ERP'
THEN ('HeO','OST')
WHEN 'TO'
THEN ('SW','OST')
END)
The reason you can't run that code is that CASE expressions can only return one scalar value, not a list of values.
Your CASE expression returns a tuple. That is not allowed in SQL. But as MySQL supports a boolean data type, a CASE expression may result in a boolean:
SELECT *
FROM tabkontaktjednani
WHERE
CASE (SELECT alias FROM tabciszam WHERE loginid = suser_name())
WHEN 'ERP' THEN typ IN ('HeO', 'OST')
WHEN 'TO' THEN typ IN ('SW', 'OST')
END;

select multiple where in a condition (nested condition) on mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to select multi-column and condition in a condition (nested condition) like a one below if it is written in a programming language:
(bulan_pembayaran == 7 && semester_payment == 1) && (bulan_pembayaran == 6 && semester_payment == 4)
How to make the above command for SQL query?
In general, you just use boolean operators just as you would in a programming language. mysql uses or/|| and and/&& interchangeably, but because || is a concatenation operator in other flavors of sql, it can be good to stick to or/and:
(bulan_pembayaran = 7 and semester_payment = 1) or (bulan_pembayaran = 6 and semester_payment = 4)
(The parentheses are only needed for clarity here; and is higher precedence than or, so without them it would produce the same result.)
But in this specific case, you can use mysql's support for list values and the in operator; this is especially helpful when there are more than a couple cases being checked:
(bulan_pembayaran, semester_payment) in ((7,1),(6,4))

Is there a better way to write REGEXP in sql to select unique transaction number R-0001 and R-AD-0001 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Am having a lot of transaction number store in my db, example I-0001, IN-0001, N-0001, S-0001, SN-0001, R-0001 , R-AD-0001, and R-AA-00001.
Every time a new row insert, I will select the maximum number of transaction numbers and + 1 to keep track of every transaction number.
Here is my initial SQL SELECT MAX(`transaction_invoice`) FROM sw_invoice WHERE `transaction_invoice` LIKE '%N-%', but I found out using LIKE to select for example maximum number of N-, LIKE SQL will also select N-0001 and SN-0001 together
Example of LIKE SQL result
To solve this I found out REGEXP might solve my issue here is example SQL
SELECT * FROM `sw_invoice` WHERE `transaction_invoice` REGEXP '^N-', this SQL works well when select maximum number of N-, but when its come to select R-0001, it will also select R-AD-0001, and R-AA-00001 which start with R-
Example of REGEXP SQL result
Is there a way any SQL can only select the maximum transaction number I require? for example if I select transaction number start with R-, its only return the maximum number of R- not R-AD-
Just remove the leading wildcard from the like:
SELECT MAX(`transaction_invoice`)
FROM sw_invoice
WHERE `transaction_invoice` LIKE 'N-%';
You don't need regular expressions for this.
If you need to handle hyphens in the first part, then you need regular expressions:
where transaction_invoice regexp '^N-[0-9]+$'

query to retrieve correct properties in the range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to retrieve all the properties in the range 2200000(min_price) to 2700000(max_price)
The query should look like
SELECT *
FROM properties
WHERE ( ((raw_min >= '{$min_price}')
OR (raw_min <= '{$max_price}'))
AND ((raw_max >= '{$min_price}')
OR (raw_max <= '{$max_price}')))
AND (..)
query should satisfy the below 6 conditions
raw_min - raw_max
(1) 1000000-2000000 (false)
(2) 1500000- 2400000 (true)
(3) 2300000-2600000 (true)
(4) 2500000-3000000 (true)
(5) 3200000-5000000 (false)
(6) 2000000-3000000 (true)
Please try this query:-
SELECT *
FROM properties
WHERE (raw_min between 2200000 AND 2700000)
OR (raw_max between between 2200000 AND 2700000)
OR (raw_min <= 2200000 && raw_max >= 2700000)
Please check I have updated the query.

How to fetch mysql data having brackets? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to fetch mysql data having round brackets e.g. ABCD(XYZ). When I run query
"SELECT * FROM tablename WHERE Column = 'ABCD(XYZ)'"
it returns an empty result. Please suggest a way. Thanks in advance!
this should work:
INSERT INTO `tablename` (`Column`) VALUES ('ABCD(XYZ)');
SELECT * FROM `tablename` WHERE `Column` = 'ABCD(XYZ)'";
Maybe 'ABCD(XYZ)' is not exactly the value of your data (for example if you inserted some whitespaces before or after it.)
You can try it with a like to find that out:
SELECT * FROM `tablename` WHERE `Column` LIKE '%ABCD(XYZ)%'";
Another possibility is that your value has been converted with htmlentities and you saved something like this:
'ABCD&40;XYZ&41;'
&40; Left parenthesis
&41; Right parenthesis