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

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)

Related

How to get get all the data from table and if same value repeated in one column that row should count once?

leave_request tablein my table emp_id column contains duplicate data 3 times. i want to select all the data from the table and the duplicate data to count as once. here i am providing the screenshot of my table structure
You can try below -
select max(id),emp_id, emp_name
from tablename
group by emp_id, emp_name

How to fetch column names runtime

My MySQL database has column names like clnt_1001,clnt_1002 .... so how can I fetch the value from all clients runtime? ,
where clients can be added later,
like where column name like '%CLNT_%' and id =2001;(all values in 'CLNT_' are integers)
You will need to query INFORMATION_SCHEMA to get the column names and use it in your SELECT query, e.g.:
SELECT GROUP_CONCAT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'your_table'
AND COLUMN_NAME like 'CLNT_%';
This will give you (comma separated) column names, you can then use the result of this query to construct the SELECT query, e.g.:
SELECT <columns>
FROM your_table;
Here, you can replace <columns> with result of the first query and get the data for all the columns.
Here's MySQL documentation on INFORMATION_SCHEMA tables.

How can i concatenate data different row from same table in one row?

I have a database name class_routine, where following and row as shown in column are there i want to get all data from row sunday and group it in all in same column, how can i do it ?
Here is the database and column name and row name
You can use group_concat and group by
select day, group_concat(routine)
from my_table
group by day;

Common Column Names from Multiple Tables MySQL

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

reference auto-increment columns?

I have 2 tables that I am working with that use the same column; one table contains the text and the other table contains the images; they use the column listing_id so that the right text shows up with the right images;
my problem is that because column listing_id is auto-increment, my first table is able to have an insert into query that is able to insert the text and then +1 the column listing_id; however the 2nd table I use another INSERT INTO query will not have the right listing_id,
because some entries for listing_id have been deleted, meaning that the 2nd table's listing_id will always be behind the 1st tables listing_id;
how do I reference the column listing_id?
You need to create an INT column called something like "parent_id" in the dependant tables that stores the id of the main table that it is referencing. When you select records from the first, you would then JOIN the tables with the auto_increment field of the first field against the "parent_id" of the second.
As MrSlayer mentions, use the newly inserted ID of the first table to update "parent_id". You should typically have a unique ID field in the second table for uniqueness, but it shouldn't be part of the relationship to the first table.
If you're unclear about how to get the id that the first table auto_increments to when you insert, use mysql_insert_id().
mysql_query("INSERT INTO table1 ...");
echo "Last inserted record_id in table1 was " . mysql_insert_id();
INSERT INTO table1 (mytextcolumn) VALUES('text');
INSERT INTO table2 (parent_id,image_name) VALUES(LAST_INSERT_ID(),'someimage.png');

Categories