MySQL How can I get data where certain column value is undefined? - mysql

I was thinking I could do that like SELECT * FROM table_name WHERE column_name=UNDEFINED, but I couldn't. I think there is a simple way to do that?

It all depends on what you think is UNDEFINED.
The most basic case is that your column_name is nullable, so your query would look like:
SELECT * FROM table_name WHERE column_name IS NULL

In mysql you compare to NULL values with something like:
SELECT * from table_name where column_name IS NULL

Related

how to select dynamic columns which are result of another query in mySQl

I have two tables.
First table have column called column_name which contains values columns names of the second table like ( column1 , column2 , etc).
I need to select columns from the second table depending on the result of query column_name from the first table .
I need help solving this issue but I cant develop it:
If You work with Oracle, i suggest You to use listagg function (http://www.oracle-developer.net/display.php?id=515)
And Query will be like:
Select column_name from all_tab_columns where table_name = name_of_your_table2 and column_name in (select listagg...)
if I understand correctly.

Search columns in mysql table?

I have a table with 300+ column. Looking for a specific column is like nightmare. Is there any query where If I would like to search for columns stats with 'grand' can be listed...
You can use show columns from table with where. Try the following query,
SHOW COLUMNS FROM tablename WHERE field like 'grand%';
Just put in your tablename after FROM and it would work.
select * from myTable where mycolumn like 'grand%'
Try this.
SELECT
table_name,
column_name,
data_type,
ordinal_position
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DatabaseName' --- the database you want to search
AND table_name = 'yourTableName'
AND column_name LIKE '%Grand' ;
This one worked for me. Its shows all columns and table in entire database
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name OR table_name LIKE '%sale%';

MYSQL: WHERE NOT LIKE '%(SELECT column_name FROM table_name)%'

i try to find %column_name% from another table_name i see the result
but he seems to ignore the %%
Why it does not work for me?
SELECT column_name FROM table_name WHERE NOT LIKE '%(SELECT column_name FROM table_name)%'
thanks,
Apologies if I do not act according to the rules, this is my first question.
Try this:
SELECT column_name FROM table_name WHERE NOT LIKE (SELECT CONCAT('%', column_name, '%') FROM table_name)
You can't just include a query in a string, so put the wildcards next to the column name in a query and return that to the NOT LIKE.
SELECT column_name
FROM table_name T
WHERE NOT EXISTS ( SELECT column_name from other_table O
WHERE O.column_name LIKE CONCAT('%',T.column_name,'%')
)

Is it possible to do a subquery on a column SELECT:ed from information_schema?

Take a look at the following query (its not working becuase templates.a.COLUMN_NAME does not exist).
SELECT a.COLUMN_NAME, a.COLUMN_COMMENT, (SELECT templates.a.COLUMN_NAME AS status FROM templates WHERE ID=5) FROM information_schema.COLUMNS AS a WHERE a.TABLE_NAME = 'templates' AND column_name LIKE 'rev%';
I want to SELECT the data in the column that corresponds to the COLUMN_NAME (where ID=5) that was selected from information_schema. Is this possible to do? Maybe I am trying to do it reverse?
I am looking for a result like this:
COLUMN_NAME COLUMN_COMMENT status
rev_body_easy Body text easy to read 2
rev_header_easy Header easy to read 1

MySQL, search a database and tell what tables contain two specific columns?

I have a database with well over 90 tables and I am trying to figure which, if any, of them have the same two specific columns. The code I am looking for would be something like this:
SHOW TABLES IN `database`
WHERE column = 'columnA'
AND column = 'columnB';`
Is this even possible?
This will give you all tables having either of the two columns, which you can browse through to find what you need.
select *
from information_schema.columns
where column_name in ('columnA', 'columnB')
order by table_name, column_Name
SELECT TABLE_SCHEMA AS `schema`, TABLE_NAME AS `table`, COLUMN_NAME AS `column`
FROM `information_schema`.`COLUMNS`
WHERE COLUMN_NAME IN('column1','column2')