If I have a query thus:
SELECT id, name, age FROM people;
I will get columns named id, name and age. Is there an option to specify "I want full table.column names in my result"? IE, ideally I'd run the above query and get the columns people.id, people.name and people.age.
If it makes a difference, the query is being run from PHP.
I do not wish to use SELECT id AS people.id (etc) as I have around 50 queries containing around 10-12 columns each. I wish to avoid manually rewriting all these queries.
The 'duplicate' question has no answer for my issue (nor it's original question as far as I can tell) other than "it's not possible".
You can use column aliases for that purpose.
SELECT id AS 'people.id', name AS 'people.name', age AS 'people.age' FROM people;
You can simply use as
SELECT id as `people.id`, name as `people.name`, age as `people.age` FROM people;
Try this.
Try this...concat your table name with column_name and get all the column from your table using information schema.......
SELECT group_concat('table1.',COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'table1';
later on if you want you can store that result in variable and use them as per your requirement.
rather you can create a dynamic query using input table name and db_name and traverse them until your goal reach :-)
Use Alias
Basically aliases are created to make column names more readable.
Sql Aliases
Example :
SELECT id AS 'people.id', name AS 'people.name', age AS 'people.age' FROM people;
Related
select substances,[table_name(i.e annex2)]
from annex2 where substances like '%methyl%'
union select substances,[table_name(i.e annex3)] from annex3
where substances like '%methyl%'
Like the code shown above, i want to add a column with a table name corresponding to its data.It's maybe a virtual column coz there isn't a column like this in any of my table structure. Could anyone help me to go through this?
You could select the table name as a string literal. Note that since you now know for a fact that there won't be any duplicates between the results from the two tables (since you're selecting a different string literal), you can use union all instead of union, and probably speed up the query a bit:
SELECT substances, 'annex2' AS table_name
FROM annex2
WHERE substances LIKE '%methyl%'
UNION ALL
SELECT substances, 'annex3' AS table_name
FROM annex3
WHERE substances like '%methyl%'
let's see I have a simple table like this:
name id
tom 1
jerry 2
... ...
And from the outside, I got a list contains the names (tom, jerry, kettie...)
I am trying to use WHERE IN clause to retrieve the id based on the name list.
I can do
SELECT id FROM mySimpleTable where name in ('tom','jerry','kettie');
So just iterate the name list and generate the contents in the parentheses.
This works, but the results is not in the input order, for example, the input is tom, jerry, kettie, the expected the result is 1,2,3, however, the output actually could be in any order.
Then how can I modify the SQL clause to make sure I get my input and output matched so that I can do the following process accrordingly. I heard JOIN may help in this situation.
SELECT id
FROM mySimpleTable
where name in ('tom','jerry','kettie')
order by field(name, 'tom','jerry','kettie')
I heard JOIN may help in this situation.
Yes it can help:
SELECT m.id
FROM mySimpleTable m
JOIN (
SELECT 'tom' AS name, 1 AS orderNum
UNION ALL
SELECT 'jerry' AS name, 2 AS orderNum
UNION ALL
SELECT 'kettie' AS name, 3 AS orderNum
) AS sub
ON m.name = sub.name
ORDER BY sub.orderNum ASC;
SqlFiddleDemo
This solution can be also used in different RDBMS. field is MySQL specific.
How it works:
Create derived table/subquery with values you need to check and ordering column
JOIN will return only rows that correspond each other based on name
ORDER BY column you've added in subquery
just select id,name from table_a where name in ('tom','jerry','happy') , you will have the combination of the input name and output id.
this entirely depends on where you're getting the list for your "in" clause.
if it's from somewhere on the outside, you probably should first turn the list into a temp table, adding an id column that indicates the order (see this answer for a start on how to do that) - and then do an inner join with it.
I did try to run your SQL query, and me for one did get the resultant output in the same order as that of the input. Well, but still it isn't necessary it would happen the same way every time, so the best way to arrange your output in a particular hierarchy is to use the ORDER BY clause. The syntax would be:
SELECT column_name
FROM table_name
WHERE conditions
ORDER BY column_name;
So in your case, the query would read as:
SELECT id
FROM mysimpletable
WHERE name
IN('tom','jerry','kettie'....)
ORDER BY id;
You can get more help with MySQL concepts here for further information.
Select
id
from mySimpleTable
where name in ('tom','jerry','kettie')
Order by id
I have a table called "Employee" with a column name called "EmployeeName" and I know that all of these different queries will give all the employees in a table:
select EmployeeName from Employee;
select EmployeeName from Employee allEmployees;
select allEmployees.EmployeeName from Employee allEmployees;
select EmployeeName from Employee as allEmployees;
They both bring up all the employees in the table, is the difference between them in terms of query efficiency or running time?
I assume that for simple queries like this, there wouldn't be much difference, but if the above were part of some much longer query, would there be a better one to use? If so, why?
you are using somerandomword as table alias there no difference between these two quires,they result same output because there only one table,
but if you have two different tables with same column name than this doesn't work because there is name conflict,at that time you have to use alias or table name.
Aliases are used to give a temporary name to database table OR a column in a table. These are created to make column names more readable.
Wiki
SELECT somerandomword.EmployeeName from Employee somerandomword;
--------------
^This is the alias name for your table
Its safer to get data from columns, especially when we use multiple tables in SELECT query, like below syntax
Syntax :
SELECT t1.col1, t2.col1 from Table_Name1 as t1, Table_Name2 as t2;
Sample Code
I have a query like this :
SELECT * FROM (SELECT linktable FROM adm_linkedfields WHERE name = 'company') as cbo WHERE group='BEST'
Basically, the table name for the main query is fetched through the subquery.
I get an error that #1054 - Unknown column 'group' in 'where clause'
When I investigate (removing the where clause), I find that the query only returns the subquery result at all times.
Subquery table adm_linkedfields has structure id | name | linktable
Currently am using MySQL with PDO but the query should be compatible with major DBs (viz. Oracle, MSSQL, PgSQL and MySQL)
Update:
The subquery should return the name of the table for the main query. In this case it will return tbl_company
The table tbl_company for the main query has this structure :
id | name | group
Thanks in advance.
Dynamic SQL doesn't work like that, what you created is an inline-view, read up on that. What's more, you can't create a dynamic sql query that will work on every db. If you have a limited number of linktables you could try using left-joins or unions to select from all tables but if you don't have a good reason you don't want that.
Just select the tablename in one query and then make another one to access the right table (by creating the query string in php).
Here is an issue:
SELECT * FROM (SELECT linktable FROM adm_linkedfields WHERE name = 'company') as cbo
WHERE group='BEST';
You are selecting from DT which contains only one column "linktable", then you cant put any other column in where clause of outer block. Think in terms of blocks the outer select is refering a DT which contains only one column.
Your problem is similar when you try to do:
create table t1(x1 int);
select * from t1 where z1 = 7; //error
Your query is:
SELECT *
FROM (SELECT linktable
FROM adm_linkedfields
WHERE name = 'company'
) cbo
WHERE group='BEST'
First, if you are interested in cross-database compatibility, do not name columns or tables after SQL reserved words. group is a really, really bad name for a column.
Second, the from clause is returning a table containing a list of names (of tables, but that is irrelevant). There is no column called group, so that is the problem you are having.
What can you do to fix this? A naive solution would be to run the subquery, run it, and use the resulting table name in a dynamic statement to execute the query you want.
The fundamental problem is your data structure. Having multiple tables with the same structure is generally a sign of a bad design. You basically have two choices.
One. If you have control over the database structure, put all the data in a single table, linktable for instance. This would have the information for all companies, and a column for group (or whatever you rename it). This solution is compatible across all databases. If you have lots and lots of data in the tables (think tens of millions of rows), then you might think about partitioning the data for performance reasons.
Two. If you don't have control over the data, create a view that concatenates all the tables together. Something like:
create view vw_linktable as
select 'table1' as which, t.* from table1 t union all
select 'table2', t.* from table2 t
This is also compatible across all databases.
I have a database with many columns and sometimes I need to select quite a few.
Selecting all columns would be too much data. So lets say that:
DESC table_name
gives ordered column names, for example (A,B,C,D,E,F,G,H,I,J....). Is it possible that instead:
SELECT C,D,E,F FROM table_name;
I do something like this:
SELECT [3:6] FROM table_name
I know it makes no difference in this example, but I need to select over 40 columns with long names.
No, you can't SELECT [3:6] FROM table_name What do you think this is, some kind of modern computer language with sequences and ranges as first class data types? :-) :-). This is SQL.
You can, as a commenter pointed out, fetch the names of the columns in the table and then programmatically generate your SQL queries. This is, of course, something a bunch of different data-access-object packages do automatically.