Common Column Names from Multiple Tables MySQL - php

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

Related

What is the right way to make a database searchable?

I'm trying to make my database searchable. Some of my tables are products, categories, subCategories manufacturedYear, model, brands. I'm using "%LIKE%" on my query and then union on tables. I get a result back, but one of the problems I cannot get the table name. One solution I thought of is creating a searchable table with 4 columns:
id, searchable term (it could be a sku,category ,subcategory etc ) and ID of the searchable term.
You can do this:
...UNION ALL SELECT 'mytable' AS tablename, term FROM mytable ... Basically, you can put a simple string value in with a virtual field in each sub select of the union. In this case I just called it tablename with a value of "mytable" the name of this example table.
So each row in mytable will have a field added named tablename with a string value of mytable. Then as you union the results, each result set (Sub-Select) could have a different string value (in tablename) in the query for their respective tables.
You can also refer to this post which is specifically on "virtual columns"
How to create virtual column using MySQL SELECT?
I don't know if I would say this is the "Right" way, but it will surly work.
Enjoy
Since you are using UNION already, why not just add the name of the table being queried as a column in each UNIONed subquery, like :
SELECT 'table1' AS table_name, id, field1, field2
FROM table1
WHERE value LIKE ?
UNION
SELECT 'table2' AS table_name, id, field11, field12
FROM table2
WHERE value LIKE ?
UNION
...
When you get the data, you can get the name of the table from where each record was returned by checking column table_name.
NB : it would also be a good idea to return the primary key of each row, to make possible further requests easier.

SHOW COLUMNS FROM table for several selected Fields

How do I Show columns for specific fields .
SHOW COLUMNS FROM core_banking_mpesa WHERE FIELD= 'id' , FIELD ='LineNo' , FIELD ='Comments'
Your error is that WHERE column1='val', column2='val' is not valid syntax.
You could use IN() to select the fields:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD IN('id','LineNo','Comments')
Or use OR:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD='id' OR FIELD='LineNo' OR FIELD='Comments'
You could try just doing a SELECT query instead, requesting those three columns:
SELECT id, LineNo, Comments
FROM core_banking_mpesa;
If you already know what the columns are, it doesn't make much sense why you would want to use SHOW COLUMNS on this table.
First of all, in the database world, COLUMN and FIELD mean the same thing. So you can't "show columns for specific fields".
However if I understand correctly, you are trying to display data from a MySQL table for a specific set of columns/fields. If that is the case, you can try this. Use SELECT query this way :
select column_name
from information_schema.columns c
where table_name = 'core_banking_mpesa';

Is there any query to get the selected column headers in MySQL?

I have the table name test_comments with 4 fields:
id
comments
guests
user
I want to fetch only the column heading comments and guests of this table
I had used show columns from <tablename> but it fetched all the column headings
is there any solution for this?
You can use a solution like this:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE 'test_comments' AND ORDINAL_POSITION IN(2,3);
The ordinal position of the column in the table. The first column in the table is number 1.
you need to fetch only the column heading comments and guests of this table so use ORDINAL_POSITION IN(2,3)

Uniquely identify same column name of two different table after join - mysql

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

Show data from all mySQL tables

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.

Categories