Postgres get all fields that are not certain values (including nulls) - postgresql

I'm looking to filter a table but, the number of queries i'm expecting differ from the result.
SELECT *
FROM table
WHERE name NOT IN ('matrix', 'filters')
That name column contains strings values and nulls. It seems like the nulls are being filtered out but, I would like them included in the result

Related

Why does Postgres choose different data solely based on columns selected?

I'm running two different queries with two unions each inside a subquery:
So the structure is:
SELECT *
FROM (subquery_1
UNION SELECT subquery_2)
Now, if I perform the query on the left, I get this result:
However, the query on the right returns this result:
How are the results differing even though the conditions have not changed in either query, and the only difference was one of the selected columns in a subquery?
This is very counter-intuitive.
The operator UNION removes duplicate rows from the returned resultset.
Removing a column from the SELECT statement may produce duplicate rows that would not exist if the removed column was there.
Try UNION ALL instead, which will return in any case all the rows of the unioned queries.
See a simplified demo.

postgres query exclude search criteria by parameter

I have a table and 4 columns there. Also I pass 4 parameters to my query. For example, if I pass only one parameter equals true, I want only first column to take part is query
sth like
select * from mytable where (case when #mypar=true then **and field1=1** end)
I want AND clauses to be included by parameter. How to do that in postgres?

Full column metadata for views?

If you query tables from dbc.columns, you will get full metadata for every column, especially data type, length, nullable/non-nullable, etc. When querying views, you only get the database, table, and column names. All the other fields are null.
If I have a view that's doing nothing but select * from table, it seems that the underlying table's metadata would propagate to the view when it's compiled to database objects. This makes sense even for calculated columns since my experiments have shown that Teradata analyzes all possible logic paths to determine a calculated column's type. Here's an example:
replace view mydb.testview as
select case when 1 = 1 then 'a' else 'aaaa' end a;
create table mydb.testviewtotable as (select * from mydb.testview) with data;
show table mydb.testviewtotable;
In that case statement, only the first condition will ever return true, so the result will always be 'a'. However, when you look at the table DDL, you can see that it calculates the column as VARCHAR(4) which proves that it analyzes all cases:
a VARCHAR(4) CHARACTER SET UNICODE NOT CASESPECIFIC
Therefore, it seems reasonable to assume that this view metadata exists somewhere even though querying that view through DBC results in nulls for all but the aforementioned columns.

Copy selected query fields name in Mysql Workbench

I am using mysql workbench (SQL Editor). I need copy the list of columns in each query as was existed in Mysql Query Browser.
For example
Select * From tb
I want have the list of fields like as:
id,title,keyno,......
You mean you want to be able to get one or more columns for a specified table?
1st way
Do SHOW COLUMNS FROM your_table_name and from there on depending on what you want have some basic filtering added by specifying you want only columns that data type is int, default value is null etc e.g. SHOW COLUMNS FROM your_table_name WHERE type='mediumint(8)' ANDnull='yes'
2nd way
This way is a bit more flexible and powerful as you can combine many tables and other properties kept in MySQL's INFORMATION_SCHEMA internal db that has records of all db columns, tables etc. Using the query below as it is and setting TABLE_NAME to the table you want to find the columns for
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name';
To limit the number of matched columns down to a specific database add AND TABLE_SCHEMA='your_db_name' at the end of the query
Also, to have the column names appear not in multiple rows but in a single row as a comma separated list you can use GROUP_CONCAT(COLUMN_NAME,',') instead of only COLUMN_NAME
To select all columns in select statement, please go to SCHEMAS menu and right click ok table which you want to select column names, then select "Copy to Clipboard > Select All statement".
The solution accepted is fine, but it is limited to field names in tables. To handle arbitrary queries would be to standardize your select clause to be able to use regex to strip out only the column aliases. I format my select clause as "1 row per element" so
Select 1 + 1 as Col1, 1 + 2 Col2 From Table
becomes
Select 1 + 1 as Col1
, 1 + 2 Col2
From Table
Then I use simple regex on the "1 row per select element" version to replace "^.* " (excluding quotes) with nothing. The regex finds everything before the final space in the line, so it assumes your column aliases doesn't contain spaces (so replace spaces with underscore). Or if you don't like "1 row per element" then always use "as" keyword to give you a handle that regex can grasp.

Getting Generated Always Columns list from DB2

Is there any way I get all column names and assoicated table names which has identity column set as generated always?
For I dentity columns I can simply use syscat.columns but how to fitler identity columns which has generated always vallue?
select identity, substr(tabname,1,30), substr(colname, 1, 30) from syscat.columns where tabschema='MYSCHEMA'"
From the above select list I wanted to filter only columns which uses generated values...
The online documentation for SYSCAT.COLUMNS. The two columns you're interested in are IDENTITY and GENERATED.
Your query will probably be something like:
SELECT TABNAME,COLNAME FROM SYSCAT.COLUMNS WHERE
IDENTITY='Y' AND GENERATED = 'A' AND TABSCHEMA='MYSCHEMA'