mysql - retrieve specific column name from table [duplicate] - php

This question already has answers here:
Get column name dynamically by Specific row value
(2 answers)
Closed 8 years ago.
I'm begginer in MYSQL.
I need to retrieve the COLUMN NAMES of columns which have value "TRUE"?
I found this code but I don't understand how to user it:
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME`='tablename'
Thanks in advance

I think this will help you MySQL query to get column names?
With the following you will get all information about the specific column
SELECT *
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename';
AND `COLUMN_NAME` = 'namecolumn'
If you want the names of all columns which have the value true in a row you could use foreach if you use php and write all keys in an array for example where the value is true
$list = array();
foreach($rows as $key => $value)
{
if($value === true) $list[] = $key;
}

This code shows all column names from a specific table, the schema INFORMATION_SCHEMA stores information about the items created on your database server(schemas, tables, columns etc.)
The statement above will show you alle columns from tablename, theres also a more simple syntax to do this SHOW COLUMNS FROM tablename (MySQL Manual)
How do you want to determine that the value of a column is true? Maybe there a two rows in your table with the values TRUE and FALSE for one column (TRUE in one row and FALSE in the other row)
With one Row in PHP
If there is only one row in your table execute the SHOW COLUMNS FROM tablename do a fetch_array on the result and then foreach and save only the keys where the value is TRUE.
If you try to store something like a configuration use a key value table and query it using where clause, this will be more effective.

Related

SQL - Show column names from a sql result

Something similar has been asked before but not answered and it's not 100% the same, what i have is an SQL like:
SELECT * FROM table1 JOIN table2;
I know that I can get the columns of a table using DESC, DESCRIBE or SHOW BUT, that function doesn't allow multiple tables neither SQL queries.
Both mysqli::fetch_assoc() and PDOStatement::fetch() can return a row as an associative array, which means that the columns are indexed by column name rather than by number. You can then use the array_keys() function to extract a list of the column names, in order, from the row.

Show only rows that contain data in one specific column [duplicate]

This question already has answers here:
MySQL select where column is not empty
(13 answers)
Closed 8 years ago.
I have a table that contains thousands of rows. I'm trying to show only rows that have information in a specific column. The column name is "vhf" the table name is "p_loc". The majority of the rows contain no info in the column "vhf" but, when I try the code below it wants to echo all my rows.
I just need the rows that contain any information in the column "vhf"
$sql = mysql_query("SELECT vhf FROM p_loc WHERE vhf IS NOT NULL");
Sounds like there are two cases you want to filter out:
the vhf column is NULL
the vhf column is empty, or an empty string
This means you need two clauses in your SELECT:
$sql = mysql_query("SELECT vhf FROM p_loc WHERE vhf IS NOT NULL AND vhf != ''");

Inserting data into first empty column cell without creating a new row

I want to add data to a table, one column at a time. Therefore I don't want to always add a new row, as the column I'm adding to may not have much data in compared to the other columns. As a result I suppose I want to add the data to the first empty cell in the specified column without necessarily adding a new row to the whole table (as there may already be enough rows).
My table has 5 columns.
I'm currently using:
$sql=("INSERT INTO [MY_TABLE] ([MY_COLUMN]) VALUES ('[MY_DATA]')");
Please could someone assist with the correct code? I know it's basic stuff but I'm new to mySQL.
Thanks in advance.
So you can use UPDATE statement with a WHERE condition to target that specific row
UPDATE table_name SET column_name = 'whatever' WHERE column_name = '' AND id = '1'
If you want to update all rows where that column is blank than simply use
UPDATE table_name SET column_name = 'whatever' WHERE column_name = ''
no need of using id in above query
Reference on UPDATE

Add column in tables [duplicate]

This question already has an answer here:
Add a new column in all mysql tables
(1 answer)
Closed 9 years ago.
I want to add a common column in all tables in a database
For example:
I want to insert the column TENANT_ID in all tables in database
How can i do that.
You use the ALTER TABLE SQL command as in:
ALTER TABLE table_name
ADD column_name datatype
If you have multiple tables you'll need to apply this to each table in succession.
More here: http://www.w3schools.com/sql/sql_alter.asp
yes you can add it
if you have list of table in array
$column_name = 'TENANT_ID';
$size = '';
foreach($all_table_name as $each_table_name) {
mysql_query("ALTER TABLE {$each_table_name} ADD {$column_name} VARBINARY({$size})");
}
above code i just skilton you have to set accourding to your code.

Can I get back default values for column names in a MySQL table? [duplicate]

This question already has answers here:
How to SELECT DEFAULT value of a field
(3 answers)
Closed 9 years ago.
I can get the column names for a table but is there a way to retrieve the default data value for each column?
Here is what I'm using to get the tables column names:
$q = $dbh->query("DESCRIBE tablename");
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
print_r($table_fields);
This prints an array of column names, but I'm trying to get the default data value for each column name also.
Another option is to go to the data dictionary and find the value in Information_Schema.Columns. This allows you to limit results to a single column.
$query = <<< endsql
SELECT Column_Default
FROM Information_Schema.Columns
WHERE Table_Schema = '$yourSchema'
AND Table_Name = '$yourTableName'
AND Column_Name = '$yourColumnName'
endsql;
Try this:
$query = "SHOW FULL COLUMNS FROM tableName";
// ...
Column Default.
I hope I helped.
Just give an index after your FETCH_COLUMN. I didn't try your code but based on DESCRIBE manual it will return 6 description columns. So if you have read this you may got the solution. I have read them and I think just by adding 4th index on your code like below will be your solution.
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN,4);

Categories