use 'not like' in mySQL show statements - mysql

I have a dropdownlist which is being populated by column names based on the table name selected by user in the previous dropdown. I am using the following query
SHOW columns from abcTableName LIKE '%name' which works
I want all to include all the columns names except a few columns. Therefore , I want the query like this
SHOW columns from abcTable NOT LIKE ('%name','%pk','%fk')
which does not work . Even
SHOW columns from abcTable NOT LIKE '%name'
does not work
Currently I run two loops to fetch the columns names- outer loop to pass the table name and inner loop to pass the parameters to the query which takes a lot of time .I want to optimize it.
Can anyone please suggest ?

You could use a more formal method:
SELECT COLUMN_NAME
FROM information_schema.columns
WHERE
table_schema = '[database]' AND
table_name = '[table_name]' AND
COLUMN_NAME LIKE '%name' AND
COLUMN_NAME NOT LIKE '%pk' AND
COLUMN_NAME NOT LIKE '%fk';

use where clause
SHOW columns from abcTable where field not like '%name'
look at Extensions to SHOW Statements
https://dev.mysql.com/doc/refman/8.0/en/extended-show.html

Related

SQL query - I don't know all identifiers

Is there a way, where I can see every parameter or identifier I can query from my database? Not the contents but the "column names"
Something like
SELECT * FROM myDb AS String
To simply get the column names and types of a table.
You could SHOW them.
SHOW COLUMNS FROM myTable;
But if you want to know the column names of your table, and only a bit of data from it (to see what it looks like).
Then use LIMIT to get only a few records.
SELECT *
FROM myTable
LIMIT 3
It's fast and easy.
But you can also just see the columns without data if you use a criteria that's false.
SELECT *
FROM myTable
WHERE 0=1
You can also use:
show create table table_name;
but as "LukStorms" mentioned, the below statement shows you the data in table format and in a pretier way
show columns from table_name;
You can use INFORMATION_SCHEMA.COLUMNS to retrieve all columns name
select column_name from INFORMATION_SCHEMA.COLUMNS where Table_Name='Your_Table'

How to convert all the column data in snowflake/database to upper case with a with a single query in Snowsql?

I would like to convert all the data if in lower case in snowflake table to upper case.
I have multiple catalogs, schemas and then tables. Would like to do this with Python.
Is there a straight query on table to convert all the data (columns) into upper case?
I have the following query, but I would like to do it for a specific data type and for only data that
is lower case(If this is faster) else for the entire table..
UPDATE MyTable
SET MyColumn = UPPER(MyColumn)
WHERE MyColumn != UPPER(MyColumn) COLLATE Latin1_General_CS_AS
How do I do it for multiple columns at once for the table??
One solution I can think of here is get all the columns from information_schema like this
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = <>
Once you get the query result of columns formulate your SELECT statement by using UPPER(<column_a>) dynamically.
This can be done in python(as this seems to be your choice here) easily.
Let me know if that helps.

nesting sql statements and selecting (MySQL)

Given the following MySQL-statement:
SELECT table_name,column_name
FROM information_schema.columns
WHERE table_schema = 'myschema'
AND column_name REGEXP 'ID$'
ORDER BY table_name,ordinal_position
I would like to select from the result all columns that contain a certain ID #.
Since I don't know the exact name of the column or table I should apply my 2nd SELECT, I need to apply my SELECT to some kind of "placeholders", I think.
E.g., if the resultset of the first request is:
('wp_links', 'link_id')
('wp_options', 'option_id')
('wp_postmeta', 'meta_id')
('wp_postmeta', 'post_id')
then the select should comprise all table_name that are in the first column of the result and should take the column_name of the second column of the result as argument to test whether it contains a certain ID #.
In other words I would like to find all columns in a certain database that are named *_ID and contain a certain ID# and know their corresponding table_name the column belongs to.
You can't do what you're wanting to in SQL but you could do it using procedural SQL, either in a procedure or an anonymous block.
Create a cursor which loops through the results of your statement above. When looping through the results, construct the string you want to execute on each table to see if the desired id exists and execute the query. You will create this string by doing something like:
SET sql_statament = CONCAT("SELECT * FROM ", v_table_name," WHERE ", v_column_name," = ", v_id_number);
This tutorial should help: http://www.mysqltutorial.org/mysql-cursor/
You want to do two different things:
Get information on your database (table and column names).
Get data from your database (values from columns).
You cannot do both at the same time. You can use some programming language firing first an SQL query to get table names and columns and then build a query or several queries to get the data.

MYSQL - List specific columns

I am trying to write a query which lists the names of the columns in an SQL table, however, I don't want all the columns - just specific ones. So, for example, if I was to put the COMMENT = 'test' for the columns which I want to list then I thought my query would be:
SHOW COLUMNS FROM `tbl_name` WHERE `COMMENT`='test'
This however throws an error.
Any ideas?
Thanks,
I think you can do this using information_schema.columns:
select column_name
from information_schema.columns c
where table_name = 'tbl_name' and
column_comment = 'test';
I think that SHOW COLUMNS can't have the WHERE clause, but you can try this:
SHOW COLUMNS FROM (SELECT * FROM `tbl_name` WHERE `COMMENT`='test')

MySql show tables query

I'm trying to select the tables names in my database using this query
show tables LIKE 'table1%'
it's working fine but at the same table I have a version of the table with some columns empty called 'table1_blank'. When I use the above statement I get every table starting with table1 including the "blank", how do I exclude 'table1_blank' from the selection?
Thanks.
You can use multiple like clauses using the where condition for SHOW TABLES:
SHOW TABLES
FROM `dbname`
WHERE `Tables_In_dbname` LIKE 'table1%'
AND `Tables_In_dbname` NOT LIKE 'table1\_%';
'_' has special meaning. 'any one character', so you need to change like this..
EDITED
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your database name'
AND TABLE_NAME LIKE 'table1%'
AND TABLE_NAME NOT LIKE 'table1\_%';