Renaming Columns in an SQL SELECT Statement - mysql

I would like to rename the columns in the results of a SELECT expression. Of course, the following doesn't work:
SELECT * AS foobar_* FROM `foobar`
As I'm new to SQL, I think I'm just missing a concept, tool, or keyword that would lead to the answer. A hint in the right direction would be appreciated. Thanks!
UPDATE
I'm looking for a generic way to do this, and MySQL-specific techniques are absolutely fine.
In short, I'm writing a tool that "exports" the results of MySQL queries to Google Spreadsheets (via the Google Data API). Some queries are joins, so to make columns unique I wanted to prefix all column names with their respective table names.

You can alias the column names one by one, like so
SELECT col1 as `MyNameForCol1`, col2 as `MyNameForCol2`
FROM `foobar`
Edit You can access INFORMATION_SCHEMA.COLUMNS directly to mangle a new alias like so. However, how you fit this into a query is beyond my MySql skills :(
select CONCAT('Foobar_', COLUMN_NAME)
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Foobar'

you have to rename each column
SELECT col1 as MyCol1,
col2 as MyCol2,
.......
FROM `foobar`

select column1 as xyz,
column2 as pqr,
.....
from TableName;

Related

How to write query to search particular string in a table which has more column fields in sql

I would like to know how to write sql query to search string in all columns in a table.
i.e in single where condition
I have column1, column2,... column50 fields in a table
Right now am using query like
select * from tblist where column1 like '%searchstr%'OR column2 like '%searchstr%' OR ....it goes on
Is there anyway to write sql query to search string in all columns
In the case of your exact query as given, we can try using IN:
SELECT *
FROM tblist
WHERE 'searchstr' IN (column1, column2, ...);
If you really need to use LIKE here, then there is no real shortcut available.
you could save on the grunt work by using the metadata tables to generate your query and then run it. Eg:
select concat('%searchstr% like ',COLUMN_NAME,' OR ')
from information_schema.columns t
where table_name='t' /*change to the table name*/

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'

write a query that applies to an entire db instead of a 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.

MySQL phpMyAdmin replace all like pattern in multiple tables

I am trying to look for a way to replace all occurrences of a particular pattern in my database across many tables and columns, for this I need to create some way to do this, it does not need to be done by a script, just some SQL code that will do this.
For example, I want to replace all occurrences of 'v2' with 'www' but have no idea how to do this.
I am not looking for a tutorial, just a bit of guidance on what to do and how to script the SQL needed.
How do I go about doing this?
Just to guide you in a direction. You can use the replace function:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%';
In an earlier post there is more information:
How can I use mySQL replace() to replace strings in multiple records?
You can use a select CONCAT(...) from information_schema.columns to generate an update query for every table-column combination, where ... is a combination of the strings used in an update query, and column names of the information_schema.columns database.
For example:
select CONCAT("UPDATE ", TABLE_NAME, " SET ", COLUMN_NAME, "=REPLACE(",COLUMN_NAME,"'[string-to-find]'","'[string-that-will-replace-it]'",");") FROM information_schema.columns where table_schema = 'your_db';
COLUMN_NAME and TABLE_NAME are columns in the information_schema.columns table, as documented by MySQL
The above query should make the result set:
UPDATE table1 SET field1 = replace(field1,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table1 SET field2 = replace(field2,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table2 SET field3 = replace(field3,'[string-to-find]','[string-that-will-replace-it]');
...
You could output the results of the SELECT statement to a file, which then becomes a script to execute. Alternatively, if you use phpadmin or any other programming language as an interface, you can cycle through the results set, and execute the value of each row in the results set.
I got the idea from MikeW's answer here, about selecting all rows where a data value exists, and from some other stack overflow answers that I have now lost track of, sadly (sorry to the original writers)
To be honest, I think this question may be a duplicate of this, in addition to this though...
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text to search, 'Text to replace it with');

MySQL SELECT only 1 field with AS alias

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;