Is it possible to write a query:
SELECT * from table1, table 2, table 3 ...
where table1.column name like table2.column name like table3.column name
We don't know the name of our column, we say any of our columns which are in table1.column name like table2.column name in like table3.column name
For example: employee id maybe is a column that is common to 3 tables, or publisher id is a column that is common to 3 tables.
I can find the foreign key or primary key with a diagram in my database, but I want to write a query that shows just columns which have a same title as column name.
No clue as to why you are doing this, so a bit hard to steer, but there are a number of tables and views in the database that describe the database.
Information_Schema.Columns is probably where you should start. Note this is meta data, if you want the content of the actual tables you'll have to go a step further, and build some dynamic sql, or equivalent.
Select Column_Name,Count(*) As NumberOfOccurences From Information_Schema.Columns
will give you the column names and how often you've used them for instance.
If you know the column name and want to know in which different table same column name is used then write SQL query as
SELECT schema_name(t.[schema_id]) + '.' + t.[name] AS Tables FROM sys.tables t INNER JOIN sys.columns c on t.[object_id] = c.[object_id] WHERE c.[name] = 'Your_Common_Column_Name'
You will get all the table list with their respective schema
To get column names:
select sc.name
from sysobjects so inner join syscolumns sc on so.id = sc.id
where so.name = 'tableName'
Related
I’ve got quite a few tables with product information. The columns on each table that I’m pulling from in this particular query have the exact same column names. I’ve been attempting to do it via a UNION ALL but for some reason it is throwing an error saying non-object but all the column names are correct.
I’m using a format that I found online. But obviously something is wrong. There are more tables; however, this is how it starts (with 2). I’d prefer not to have to code each select statement in the union with unique table abbreviations if I don’t have to.
I don’t have to use union if there is a better method.
All tables share data on Product_Categories and Product_Sub_Category.
The only thing unique to each table is id and part_number.
SELECT f.id,f.part_number,f.cat,f.subcat,f.table_name FROM
(
SELECT t.id,t.part_number,psc.name as subcat,c.name as cat, c.table_name FROM Steel_Strapping as t JOIN Product_Sub_Category as psc ON t.subcat = psc.id JOIN Product_Categories as c ON psc.category = c.id ORDER BY c.sort_order,psc.sort_order,t.sort_order
UNION ALL
SELECT t.id,t.part_number,psc.name as subcat,c.name as cat, c.table_name FROM Product as t JOIN Product_Sub_Category as psc ON t.subcat = psc.id JOIN Product_Categories as c ON psc.category = c.id ORDER BY c.sort_order,psc.sort_order,t.sort_order
) f
My end result is one full list of all products sharing column names. Ex: $result[‘part_number’] will pull part numbers from ALL tables listed in union.
I found the solution when playing around with code. I had to add parenthesis (select...) UNION JOIN (select...) inside the parent select statement
I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table
I am bit lazy to write all the column names from all the tables when joining more tables while creating a view. Struggling with a problem of duplicity in column names (I use column name 'id' in all tables).
CREATE OR REPLACE VIEW new_table AS
SELECT * FROM a_table a
LEFT JOIN b_table b ON a.specific1_id = b.id
LEFT JOIN c_table c ON a.specific2_id = c.id
WHERE 1;
I get an error - #1060 - Duplicate column name 'id'
Solution is write all the column names instead of * .... but its too much work, right?
Thanks for hints,
Zdenka
Let's say I have some db tables created in SQL Server, those could be named Table1, Table2, Table3, Table4 and Table5 (but could be more).
Now, what I want to achieve is to possibly mark the columns in those tables that could possibly indicate a candidate I could use for joining the tables.
Is this possible somehow with SQL? Or is there any SW Tool which could create a database model upon the data/tables which would join the tables with same column names?
Example could be with Business Intelligence table structures: I could have 5 tables and wanted to decide which dimension is shared for fact tables etc.
You could view all the columns with a certain piece of text in it, such as "%ID"
Use this script:
SELECT t.name As TableName, c.name AS ColumnName
FROM sys.tables AS t
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name Like ('%ID') --You can change ID to anything you need
AND t.is_ms_shipped = 0
AND t.name NOT LIKE '#%';
A similar question was posted here: How to select column names from multiple tables in SQL Server 2000-2008 that are in a set of names
I am relativly new to the SQL language. I can do a basic select, but for performance increase, I'd love to know if it is possible to merge the two queries I am doing at the moment into one.
Scenario: There are two tables. Table one has a few columns, one of them is a VARCHAR(45) named 'user', and another one is a INT which is called 'gid'. In the second table, there is a primary key column called 'gid' (INT) and a column called 'permissions' which is a TEXT column and it contains values seperated by ';'.
I have a user name, and want the text in the permissions column. The current way I do it is by fetching the gid of the first table, then doing a second query with the gid to get the permissions.
I've heard there are other ways to do this, and I have searched on Google, but I'm not sure what I should do.
EDIT:
Like this:
select t2.permissions
from table1 t1, table2 t2
where t1.user = '<SPECIFIED NAME>'
and t1.gid = t2.gid;
or you could use INNER JOIN syntax:
select t2.permissions
from table1 t1
inner join table2 t2 on t1.gid = t2.gid
where t1.user = '<SPECIFIED VALUE>'
To do this you use a JOIN. A join connects two tables in a select statement.
Like this
select *
from usertable u
join permissiontable p on u.gid = p.gid
This will give you all the columns from both tables with the id column joined. You can treat the joined table just like any table (eg select a sub-set of columns in the select list, add a where clause, etc).
You can read more about joins in any intro sql book or doing a google search.