select multiple where in a condition (nested condition) on mysql [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 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))

Related

MySQL Query to remove data between 2 specific strings [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 2 years ago.
Improve this question
I'd like a MySQL query to achieve the following :
I have 10000 rows that look like this
Source 1::https://domainxyz.com//public/dist/s2q1sd65az7r/index.html||Source 2::https://domainabc.com/embed-8eel8v83lefs.html||Source 3::https://domainqsd.com/v/w1qy8g81w6||Source 5:://domainwxc.com/embed2.php?link=5fUE7Mo%25
Only the links are different from one row to another
I'd like to remove all content related to Source 1
That content would be anything between "Source 1" and "||" (including the removal of the string "source 1")
So in my exemple, that would removing this :
Source 1::https://domainxyz.com//public/dist/index.html
PS : it also possible that "Source 1" data has no "||" at the end (when the data is at the end of the ligne)
You can use regexp_replace():
select regexp_replace(concat(str, '||'), 'Source 1::[^|]*[|][|]', '')
from (select 'Source 1::https://domainxyz.com//public/dist/s2q1sd65az7r/index.html||Source 2::https://domainabc.com/embed-8eel8v83lefs.html||Source 3::https://domainqsd.com/v/w1qy8g81w6||Source 5:://domainwxc.com/embed2.php?link=5fUE7Mo%25' as str
) t
Or alternatively as:
select regexp_replace(str, 'Source 1::[^|]*([|][|]|$)', '')

SQL plages de dates [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 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

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.

mysql return a fixed number of queries [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 9 years ago.
Improve this question
I have 3 mysql queries in php (one for each category/widget), all 3 queries together should output 3 * 6 = 18 results. So if one query returns less, more results from the other queries should be used.
How can i do that? Thanks.
Instantiate 2 variables , keep count of selected rows and execute query using these counters
for example this will be your first query execiton
$default = 6;
$limit= $default;
$some_result = mysql("SELECT * FROM table LIMIT $limit");
then you can use this for every query-limit
$limit = $limit - count($some_result) + $default;

MS Access Script Conversion to T-Sql script [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 9 years ago.
Improve this question
Can i use nested Case statements in t-sql as i have done and also "*" used for multiplcation in the access query can it be used in the same way in T-sql for example
([Col2]*[col3])
MS Access:
IIf(IsNull([Col1]),([Col2]*[col3]),([col2]*[col3]/[col1])) as Column
T-Sql:
Case When [Col1] Is Null then ([Col2]*[col3])
else ([col2]*[col3]/[col1]) end AS column
Ms Access:
IIf(Left([col],1)=3,"Tran",IIf(Left([ss],1)=7,"Con","Sto")) AS [col]
T-sql:
(Case When (Left([col],1)=3) then 'Tran' else (Case When (Left([col],1)=7) then 'Con' else 'Sto' end )end) AS [col type]
Your 2nd TSQ should look like this:
CASE
WHEN LEFT([col],1)=3 THEN 'Tran'
WHEN LEFT([col],1)=7 THEN 'Con'
ELSE 'Sto'
END AS [col type]
As far as # you don't need that in SQL, just put the date value in single quotes correctly formatted.