MySql show tables query - mysql

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\_%';

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'

use 'not like' in mySQL show statements

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

mysql | show tables with specified name

I have a tables in my database and would like to select specified tables by name:
abc_dshop_cats
abc_eshop_cats
abc_shop_cats
abc_shop_mods
abc_shopp_cats
How can I get these tables: abc_shop_cats and abc_shop_mods using the command below:
SHOW TABLES LIKE '%shop%'
And the command above is not working as return me all tables...
Thanks!
You could do like:
show tables like '%\_shop\_%'
This will get
abc_shop_cats
abc_shop_mods
but not
abc_dshop_cats
abc_eshop_cats
abc_shopp_cats
select table_name
from information_schema.tables
where table_name like '%shop%'
just use a normal SQL WHERE statement to do it.
SHOW TABLES WHERE Tables_in_mydbname LIKE '%_shop_%'

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 statement search all tables for one specific column?

I am using the command line in MySQL on a Linux box.
I am logged in as root.
I need to find a field that in a table somewhere (in this schema there are 400+ tables)
Field is called 'discounts' or 'discounts'
I just need to find what table that specific field is in and if it is in more then one table with that feild or not.
Thanks for your help guys.
Edit
so the db is called magento and the tables are in there.
Try this:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE LOWER(COLUMN_NAME) = 'discount' or LOWER(COLUMN_NAME) = 'discounts';
Try this:
SELECT * FROM information_schema.columns WHERE column_name = 'discounts'