I have a ton of tables that vary in columns by a small amount. I want to be able to select all the data from every table and just display null values (or blanks) when one table doesn't have a column from another table.
I know this can normally be done using the JOIN operator when you have tables that have relationships between each other, but my tables have no relationships between each other except that they have a lot of common column names.
Anyway, this is the closest I can think of: fiddle.
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.columns
WHERE TABLE_SCHEMA = 'database_name'
ORDER BY TABLE_NAME
This gives you list of all table names. You can then run queries on all of those.
Related
Hi i have multiple tables of products like product_shirt, product_pants etc and on top of it there is a general product table. Anyways i have came to a situation where i need to make a UNION query by selecting some fields from all the tables that are common like . color field is both in shirts and pants and bags etc ...
Is there any query for getting column names that exist in multiple tables ? I am trying to google it but haven't found any related result
Thanks
Updated:
Table names : prod_rugs, prod_furniture, prod_accessories, prod_generic.
Colum names : color, size and (I want to know all the column names that exisits in all above mentioned table)
I do not need the value of color or size... i need to know which fields are common among these tables
Yes, you may use INFORMATION_SCHEMA for that:
SELECT
column_name,
table_name
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_schema='test'
GROUP BY
column_name
HAVING
count(1)>1
You should replace test to name of your database, or course. Query above will result in column names and table names, which occur more than once - so, more, than in one table (because, obviously, one table can not have column with same name more than once)
If you want some specific table & columns name, then add corresponding condition:
SELECT
column_name,
table_name
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
-- database name:
table_schema='test' &&
-- table names:
table_name IN (
'prod_rugs',
'prod_furniture',
'prod_accessories',
'prod_generic'
) &&
-- column names:
column_name IN (
'color',
'size'
)
GROUP BY
column_name
HAVING
count(1)>1
You need to use sql join to do this.Check this link
http://www.w3schools.com/sql/sql_join.asp
I have 2 tables. suppose a & b
a has id, name, roll. b has id,group,name
This name column data are not same. How can I select and uniquely identify them?
I know about
SELECT a.id,a.name,a.group FROM a,b ............
I know this. But this is an example. I am working with huge amount of data with 20-30 columns in each table. So I don't want to write the column names I need to select rather I want to write the names that I want to exclude.
Like
SELECT * Except b.name............
OR is there any way to uniquely identify after join. Like
.......... a,b WHERE a.name as name1
Please don't ask why those column names are same. I admit it was a mistake. But it's already implemented and heavily used. So finding another way. Is there any simple way to exclude a column while merging them?
Well, you can't write the names you wish to exclude. That is not how SQL works.
However, if writing out 20-30 column names is that much of a burden, you can use information_schema.columns. I write it that way, because 20-30 column names is not particularly large and writing them out is probably less effort than writing the question.
But, back to the solution. It looks something like this:
select concat(c.column_name, ' as ', 'a_', column_name, ', ')
from information_schema.columns c
where table_name = 'a' ;
You might want to include the table schema as well.
As an IDEA, what you can do is, if you want to avoid columns of specific table & your statements have multiple table, you can try following,
Suppose you have 20 columns in table a & 5 columns in table b, you want to avoid col2,col3 & col4 of table b. Standard method is that you should write name of all columns of table a & required columns of table b. But you can avoid to write long list of 20 columns of table by writing a.* & then type required columns of table b. Please see below statement.
Select a.*,b.col1,b.col4,b.col5 from a,b
But if you require to exclude some columns from both table, then I think there is no other way than writing all required column names from both table.
There is no way to exclude a column in SQL SELECT Statement, you can only select a column. You can give alias name to columns while selecting them like below, so that you can identity columns using those alias names.
SELECT a.id as [column1],a.name as [column2],a.group as [column3] FROM a,b ............
There is no way to exclude a specific column but you can avoid to write all columns name and easy your job by below steps-
Step1: Execute below query-
SELECT a.*,b.* FROM a,b ............limit 1;
Step2: Export it into csv format with headings.
Step3: Copyp first (heading) row from csv.
Step4: Delete columns, those are not required and use other columns in your query.
There's only one waY i could see-
first create a temorary table
CREATE TEMPORARY TABLE IF NOT EXISTS mytable
(id int(11) NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM;
then put your column in temporary table-
SELECT * INTO mytable
FROM YourTable
/* Drop the cloumns that are not needed */
ALTER TABLE mytable
DROP COLUMN ColumnToDrop
/* Get results and drop temp table */
SELECT * FROM #TempTable
DROP TABLE #TempTable
Using PHP, when I'm about to delete a row from a table I want to able to determine if it's PK value is used by any other table
I have a table where it's PK may be FK in one or several tables. At first hand I don't know how many tables are related.
What could be the most efficient and shortest way to achieve what I'm looking for?
My first approach is to get which tables are related to, doing something like:
select table_name, column_name from information_schema.key_column_usage where
table_schema = 'my_schema' and referenced_table_name = 'my_table' and
referenced_column_name = 'id_field'
Then loop through that list and for each table do something like:
select count(*) as total from table_name_from_loop where id_field = my_value
So if I any of those count() is greater than 0, then that row is in use.
Any suggestion on how to perform this task? I prefer some kind of check instead of trying to delete and catch some error
EDIT 201309091540: Anyone?
If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;
I had my query set up the other day as so
$query = "SELECT card_id,title,description,meta_description,seo_keywords,price
FROM cards,card_cheapest order by card_id";
As you can see, I was selecting card_id,title,description,meta_description,seo_keywords from the table cards, and price was coming from cheapest_card. They both have the card_id in common (in both tables). However, I ran into a bit of an issue. When I run the query in navicat lite, I receive an error "card_id is ambiguous". Was I doing something wrong?
When 2 or more tables have a column that is named the same, you have to qualify the table you want the column to be from.
i.e.:
$query = "SELECT cards.card_id,title,description,meta_description,seo_keywords,price
FROM cards,card_cheapest order by card_id";
Furthermore, do you really want to run the query this way, without a WHERE/JOIN-clause to define how to JOIN the two tables?
$query = "SELECT cards.card_id,title,description,meta_description,seo_keywords,price
FROM cards,card_cheapest WHERE cards.card_id = card_cheapest.card_id
ORDER BY card_id";
When you have the same column name in two tables you're selecting from, you have to prefix the part in the SELECT with one of the table names (it doesn't matter which if it's the same data)
such as SELECT cards.card_id, ...
EDIT: However, cularis's answer is much more explanatory than mine, and take note about joining the two card_id columns if you want to get correct results.
When you run queries that get information from multiple tables with shared field names you need to specify from which table you want to extract what field. You do this by specifying the table name before the field name.
In your case you have two options:
cards.card_id or card_cheapest.card_id.
Also I agree with #cularis, you are probably better of doing a join, but still you will need to specify which card_id you want to select: the one from cards or card_cheapest.