Laravel 6: Not using column on the where clause - laravel-6

I want to achieve something like this:
select * from my_table where (id = 200 OR 1 = 1)
How can I do it in Laravel eloquent?
It's a little bit weird request, but it's necessary because if the value of the request variable passed is 1, then return all records, otherwise return the record where id is equal to that variable.

Related

Postgres incrementing null value in a column

In Postgres 9.3 when a field in a table has a null value the following expression doesn't work:
update table_statatistic set members = members + 1 WHERE user_id = $1;
However when the field has an integer value then this query increments it by 1 without a problem.
The questions are:
Why is this happening.
How to fix it.
you need to use coalesce for checking null values
update table_statatistic set members = coalesce(members, 0) + 1 WHERE user_id = $1
Use COALESCE() instead:
The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display
UPDATE table_statatistic SET members = COALESCE(members,0) + 1 WHERE user_id = $1;

Select Where Like regular expression

I need to create a SQL Query.
This query need to select from a table where a column contains regular expression.
For example, I have those values:
TABLE test (name)
XHRTCNW
DHRTRRR
XHRTCOP
CPHCTPC
CDDHRTF
PEOFOFD
I want to select all the data who have "HRT" after 1 char (value 1, 2 and 3 - Values who looks like "-HRT---") but not those who might have "HRT" after 1 char (value 5).
So I'm not sure how to do it because a simple
SELECT *
FROM test
WHERE name LIKE "%HRT%"
will return value 1, 2, 3 and 5.
Sorry if I'm not really clear with what I want/need.
You can also change the pattern. Instead of using % which means zero-or-more anything, you can use _ which means exactly one.
SELECT * FROM test WHERE name like '_HRT%';
You can use substring.
SELECT * FROM test WHERE substring(name from 2 for 3) = 'HRT'
Are the names always 7 letters? Do:
SELECT substring (2, 4, field) from sometable
That will just select the 2-4th characters and then you can use like "%HRT"

Negate a bind variable

I am using APEX 5.0 and I have a query such as:
Select * FROM table where condition = :BIND_VARIABLE
I have a list which dynamically fills in the bind variable. The list has two values which the bind variable can take:
Value 1
Everything except value 1
'Everything else' value which is returned is user controlled so I can't have an EXISTS or IN because I do not know all the values that will be in there.
Is it possible to do something like
Select * FROM table where condition = !'Value 1'
I don't remember exact syntax for oracle substring, but you can try something like:
Select *
FROM table
where
(condition = :BIND_VARIABLE AND SUBSTR(:BIND_VARIABLE, 1, 1) <> '!')
OR
(condition <> SUBSTR(:BIND_VARIABLE, 2) AND SUBSTR(:BIND_VARIABLE, 1, 1) = '!')
so, if your value starts with ! symbol - you will look for all except passed value

"!="/NOT perhaps not working properly in SQLite

I have a table with about a hundred rows. It has a column is_gallery that contains either 1, 0, or NULL. If I do...
SELECT * WHERE is_gallery != 1
or
SELECT * WHERE NOT (is_gallery = 1)
it excludes the rows where is_gallery is null. I can manage to get a proper response if I do
SELECT * WHERE (is_gallery = 0 OR is_gallery is null)
But shouldn't the "!=" or NOT work? Isn't there a way to just return the rows where is_gallery doesn't equal 1 without testing for every other possibility?
You can use the IS and IS NOT operators instead of = and !=. These treat NULL like a normal value.
SELECT * FROM yourTable WHERE is_gallery IS NOT 1
The best thing to use is coalesce as in:
SELECT *
WHERE coalesce(is_gallery,0) != 1;
what coalesce does, is replaces any null value in that column with the second parameter. In the example above, any nulls in the "is_gallery" column will be replaced with 0 before it is compared with 1. So will of course return true.
On NULL realize that a NULL value isn't equal to ANYTHING - not even NULL itself. It cannot be compared - so when "comparing", it always will return FALSE. On NULL, it has a special operator which is "IS NULL" or "IS NOT NULL"

DB2 Increment a value by a certain amount

I need to write a query that increments a value in a table by 3 when run.
I would like to do something like this but this doesn't work.
UPDATE table
SET value = (SELECT value
FROM table
WHERE condition = true) + 3
WHERE condition = true
As in the title this is a DB2 database, any ideas?
EDIT: Actually this does work, could also do the + 3 in the select. I just had some stuff in the wrong place with the casting I had to do
Thanks in advance
I think what you are looking for is simply
UPDATE table SET value = value + 3 WHERE condition = TRUE
Does that work?
If you want all rows where condition = true to have (for example) 3+ the max value of any row for which the condition is true, use this:
UPDATE table SET value =
(
SELECT MAX(value)
FROM table WHERE condition = true
) + 3
WHERE condition = true