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;
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.
I'm relatively new to SQL and I've been having trouble trying to figure out how to select rows from a table that matches a string.
The name of the table uses the current month and year.
It looks like this:
XXXX.xxx_YY_MM
where XXXX is the database, YY is year, and MM is month.
So normally the query would just look like this:
select * from XXXX.xxx_16_05;
However, I want it to be able to change depending on the date.
So I thought this would work:
select * from (select concat('XXXX.xxx_',date_format(now(), '%y_%m'))));
The concat bit gives me something that looks exactly like the name of the table. But it doesn't work and I'm not sure why. It says every table must have it's own alias. I'm not sure what to do about it.
Alternatively, I was thinking maybe something like this would be ok
select * from (select * from information_schema.tables where table_name like concat('%logsms_',date_format(now(), '%y_%m'),'%'));
But it doesn't work either. What should I do? There is only one table with a name that matches the string.
You can't use an expression for the table name in SQL. You need to use a stored procedure that creates dynamic SQL and executes it using PREPARE and EXECUTE.
SET #sql = CONCAT('SELECT * FROM XXXX.xxx_', DATE_FORMAT(NOW(), '%y_%m'));
PREPARE stmt FROM #sql;
EXECUTE stmt;
A database design that requires this seems like a poor decision. You should have a single table where the date is in a column, not separate tables for each month.
If you're running the query from a programming language, you can use its own date functions to constructure SQL.
Please find the following query to retrieve all table by matching string.
select table_schema as database_name,table_name from information_schema.tables
where table_name like '%your_table_name%' order by table_schema, table_name;
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'd like to select all columns in a table, but give only one of them an alias.
I have this table:
id
base
window
thickness
shading
shape
... and many more columns
Now, I want to select the base with an alias, like so: SELECT base AS structure.
But I want to select all the other fields as well, without having to type them all out.
I tried SELECT *, base AS structure, but it gives me an error.
In truth, that's not even really what I want, because I don't want base to show up at all.
Is this at all possible?
No, it isn't. Suck it up and type them all out :)
No.
You either list the ones you want, or you say "all" by writing *.
These are the two options at your disposal.
Laziness: begone! (And, let's face it, if you really need this alias, then your field is probably named wrong in the first place...)
Ultimately, you could create a VIEW to do this job transparently, but then you'd have to keep updating it as you ALTER your original table.
I was trying to avoid bringing this to your attention, but this answer does demonstrate a rounadabout way:
SET #sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_exclude>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
(Replacing <table>, <database> and <columns_to_exclude>).
I wish to re-iterate, though: don't. Something's wrong if you have to do this.
If you don't mind also having the old name you can do something like this:
SELECT MyTable.*, MyTable.base AS structure
maybe there is a better way to solve your problem, at least the following answer works for you if you are not too lazy:
SELECT id, base AS structure, window, thickness, shading, shape ... from yourTable
as far as I know (and a check with the MySQL documentation confirmed) that it's not possible to list all the column with the original name except one, at least using *.
Inefficient, but clean, try left joining the same rows to themselves then selecting from the left join the desired column with its new name.
ie.
SELECT table_1.*, table_2.base AS structure
SELECT
table_1.*,
table_2.base AS structure
FROM
table1 AS table_1
LEFT JOIN table2 AS table_2
ON table_1.id=table_2.id
Query:
SELECT firstName AS firstStudent FROM student LIMIT 1;