I ma testing Dynamic Multiple Data Source so that I need to show tables in both database as format below:
database|table |column1|column2
master |customer|data1 |data2
database|table |column1|column2
replica |order |data1 |data2
At the moment, I use code as below, it's not pretty..., do you have any idea?
use master;
select database();
select * from master.customer;
select * from master.customer_order;
use replica;
select database();
select * from replica.customer;
select * from replica.customer_order;
You can add the database and table names as fix strings to the sql statement:
select 'master' as `database`, 'customer' as `table`, master.customer.* from master.customer
Since database and table are reserved words, make sure they are enclosed by backticks (`).
Related
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'
Is it possible to write a query that applies to an entire database as opposed to one table.
So instead of usin:
select * from table_name where columnName = ?
Can I say select * db_tables from from db where the table contains the column A?
Is is possible?
thanks
You sure can!
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%A'
You cannot do SELECT * FROM all the tables, but you can run a query aganist multiple tables using other statements in SQL.
Let's say I have a couple tables which uses same name pattern such as:
my_1_some_table
my_2_some_other_table
my_3_and_another
Can I query these tables only with my_1_ ?
Thanks in advance for any tip...
Edit:
I think I wasn't clear enough. I just want to query a table like:
SELECT * FROM my_1_some_table;
But without using the full name. For example something like this:
SELECT * FROM my_1%
No you cannot. You have to specify the name of the table when you are selecting the data from it. The best you can do is to create an alias for the tables but then too you need to create the unique alias name for all the tables you have.
No, you cannot. At least not without aliases. You can use an alias so as to reference the table easily.
For instance:
SELECT my_1.field, my_1.another_field, my_2.field_from_other_table
FROM my_1_some_table as my_1
JOIN my_2_some_other_table as my_2 on (...)
You could first get the tables you want using a statement:
SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'my_1_%';
Then create a query (stored in preparedQuery) using what you retrieved.
SET #myQuery = preparedQuery;
PREPARE query FROM #myQuery;
EXECUTE query;
DEALLOCATE PREPARE query;
As R.T. answered, you cannot - at least not directly.
One dirty trick you could use, however, if you're only interested in querying, is to create a view:
CREATE VIEW my_1 AS SELECT * FROM my_1_some_table
And then you could query from it:
SELECT * FROM my_1
There is easy workaround just create a simple view with small names and then you can use those smaller view names in place of tablename....
Eg.
-- create views with smaller name...
create or replace view my_1 as select * from my_1_some_table;
create or replace view my_2 as select * from my_2_some_other_table;
create or replace view my_3 as select * from my_3_and_another;
-- use these view name in your query...
select * from my_1;
select * from my_2;
select * from my_3;
If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.
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\_%';