Mysql can we get all rows with SELECT IN query - mysql

I was hoping to call a dynamic query in mysql-nodejs
the query is something like this
select a
from table
where startdate = ?
and prjId in (?)
and loc in (?)
there will be 3 parameters
`startdate` string
`prjId` array
`loc` array
my question is if the array prjId is null it should get all project id
same in the case of loc also
both are null
one is not null
both arrays are not null
Simply put
select * from table where name in (?)
can it return all rows if nothing is given, which I checked (wont work)
But does there exist something like that
thanks

Related

Why does <> 'null' works in MySQL?

Hope the question is not too generic. Couldn't find anything on the site or in SQL documentation:
While coding, i tested this, and to my surprise it worked:
SELECT * FROM cal_entry WHERE cal_entry.parent_id <> 'null'
It actually shows the rows without the ones with NULL values (these are real NULL values in database, not strings with 'null' inside).
According to the docs, I should have used NOT NULL, of course. By the way, it doesn't work with = 'null', like it is correctly stated in the docs.
Can someone explain that?
You are selecting all rows where <> 'null' is true.
Comparing(equals or not-equals) to null is null, so if a row where cal_entry.parent_id is null, your condition will be false/null.
So your query gets all rows that are not null, nor contain the string 'null'.
(Note, you could just as well have written <>'something_else')
Assuming parent_id in an int column the query will return all non-null, non-zero rows:
SELECT *
FROM (
SELECT NULL AS parent_id UNION ALL
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 2
) AS cal_entry
WHERE cal_entry.parent_id <> 'null'
-- returns 1 and 2 but not 0!
When comparing a number to string MySQL will convert the string to number. Some examples:
'null' becomes 0
'asdf' becomes 0
'1asdf' becomes 123
'1' becomes 1
Your query will behave like:
WHERE cal_entry.parent_id <> 0
this operator give you result of not equal to. ex. $var != null.
we write in mysql as <>. this is kind of validation that the value shoud never be equal to null.
When working with null following two statements should always be taken note of -
An expression can be null, but it can never be equal to null.
Two nulls are never equal to each other.
So, in your query wherever there is a comparison null<>null it returns true by second statement.
Also, always account the possibility that some rows might contain null -
Select * from cal_entry where cal_entry.parent_id!=10
This query would leave out the rows with null entries. Instead use -
Select * from cal_entry where cal_entry.parent_id!=10 or cal_entry.parent_id is null

Selecting NULL values of the particular date

You can get the particular data if date exist in a table row, what "IF" it's "NULL"?
Can you get all NULL data of today? Is it possible using mysql query?
Edited:
I have a question, can you retrieve data that are not recorded for a certain date?
For example:
Janice Student 2017-xx-xx
Paul Student 2017-xx-xx
Mika Student ------
You can retrive those records with date, but can you retrive data that have no values? Is it possible using mysql query? or conditional x looping statement using PHP?
To locate information where there is no value stored use IS NULL e.g.
select * from yourtable where column_name IS NULL
If the column is a string of some type then it might be an "empty string", which looks like no value but isn't NULL, so for this use:
select * from yourtable where column_name = ''
or in combination:
select * from yourtable where ( column_name IS NULL OR column_name = '' )

mysql check value with null

In mysql table real_account_id's data type is int(11).
I want to fetch all records with NULL values.
I am using this simple query.
SELECT * FROM `customer_payment_options` WHERE real_account_id = NULL
But, it's not giving me any results. There are 2 records in database with NULL value.
I also tried with,
SELECT * FROM `customer_payment_options` WHERE real_account_id = 'NULL'
Why is it like this?
Thanks.
NULL is equal to nothing including NULL. That's why you must use IS NULL:
SELECT * FROM `customer_payment_options` WHERE real_account_id IS NULL

Get records where json column key is null

I have table users with json column details.
I want to fetch all user records where details["email"] is null or email key doesn't exist.
This doesn't work:
SELECT users.* FROM users where details->'email' IS NOT NULL;
ERROR: operator does not exist: json -> boolean
use brackets (). Looks like compiler tries to see it like details->('email' IS NOT NULL). So you can fix it like this:
select *
from users
where (details->'email') is not null
sql fiddle demo
actually, to get records where details["email"] is null or email key doesn't exist, you can use this query:
select *
from users
where (details->>'email') is null
as described in this answer.
You need to use the ->> operator for that:
select *
from users
where (details->>'email') is not null
Because the -> operator returns 'null'::json (and not sql NULL) if the key is exists but with a json null value.
http://sqlfiddle.com/#!15/76ec4/2

I need some help getting MySql to output some results using a subquery

I'm storing a list of numbers inside a table as a varchar(255) and want to use this list in another query's "IN() clause.
Here's what I mean:
Table Data:
CREATE TABLE IF NOT EXISTS `session_data` (
`visible_portf_ids` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `session_data` (`visible_portf_ids`) VALUES
('45,44,658,659,661,45,44,658,659,661')
I want to run a query like this to return a list of portfolio's "QUERY #1":
SELECT portfolio_hierarchy_id, account_id, name, leaf_node_portf_id
FROM portfolio_hierarchy
WHERE account_id = 1
AND leaf_node_portf_id IN
(
(SELECT visible_portf_ids
FROM session_data
WHERE username = 'ronedog')
)
ORDER BY name ASC
The result of the query above returns only 1 row, when there are a total of 3 that should have been returned.
If I run the subquery alone like this:
(SELECT visible_portf_ids
FROM session_data
WHERE username = 'ronedog')
it will return a list like this:
45,44,658,659,661,45,44,658,659,661
But, when I run Query #1 above, only one row of data, which is associated with the "visible_portf_ids" of "45" is returned.
If I replace the subquery with hard coded values like this:
SELECT portfolio_hierarchy_id, account_id, name, leaf_node_portf_id
FROM portfolio_hierarchy
WHERE account_id = 1
AND leaf_node_portf_id IN (45,44,658,659,661,45,44,658,659,661)
ORDER BY name ASC
then I get all 3 rows I'm expecting.
I'm guessing that MySql is returning the list as a string because its stored as a varchar() and so it stops processing after the first "visible_portf_ids" is found, which is "45", but I'm not really sure.
Anyone got any ideas how I can fix this?
Thanks in advance.
You should think about restructuring your tables storing each value in a new row, instead of concatenating them.
Until then, you can use the FIND_IN_SET() function:
AND FIND_IN_SET(leaf_node_portf_id,
(SELECT visible_portf_ids
FROM session_data
WHERE username = 'ronedog'
LIMIT 1)
) > 0
Unfortunately MySQL does not have a function to split a delimited string. Your IN argument is a single string with the result of your subquery. The reason it works when you hard-code it is that MySQL is parsing the values.
I suggest that you redesign your data base to store the visible ports list as separate rows in a separate table. Then you can retrieve them and use them in subqueries like you tried.