Show column name MySQL - mysql

Is there a way to get a column name from table where all values in this column are the same!
Example! IF i would there would be a such a code it would return answer 'Works'
Table1
ID Name Works
1 Andre Yes
2 John Yes
3 Stewart Yes

I don't know if the columns of the table are known. If not, you could be able to get them by:
desc Table1;
or if you are using higher version of MySQL, you could use:
select column_name from information_schema.columns where table_schema='your_schema' and table_name='Table1';
Then you try the following statement with parameters #column being replaced with the column names retrieved from the above statement:
select count(*) from (select count(*) as c from Table1 as t group by t.#column) as sub;
If the result is 1, the column is what you want. The result means how many distinct values this column has.
I suppose you will have to use a kind of programming language or stored procedure. You are not likely being able to achieve that with one single SQL statement.

I'm not sure if it's possible to run from MySQL itself but you can do this from your scripting engine.
You can run a loop on each column and do a query:
SELECT
COUNT(DISTINCT column_name)
FROM table_name;
And see you get 1 record in the results

Related

SQL SELECT two specific names from table not working

Name
UID
Late
Tin
ABC
0
Bob
ABC
0
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
This query returns nothing for me and only works when I want one name.
I could use the SELECT * but for this case I would like to call specific names in the query?
For this use In clause.
SELECT * FROM logs WHERE Name IN ('Tin', 'Feryal');
You can also use or clause
SELECT * FROM logs WHERE Name='Tin' OR Name='Feryal'
To add on to Amit Verma's answer.
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
Reading that SQL statement out loud, it sounds like this:
I want to select all the rows from logs where the name is equal to Tin AND the name is equal to Feryal.
You can quickly see from that statement, the reason why 0 rows are returned, it's because that is impossible! You cannot have somebody named both Tin and Feryal at the same time unless they are some bizarre super-positional being and the datatype in the table somehow allows for that.
Amit covers the rest.

How can I retrieve the column names from an empty MySQL select query result

Is there a way to retrieve the column names of a query that returns no data?
The result of this query would be empty.
Is there a way how to find the column names when there's no result?
Please note that I'm aware of solutions using DESCRIBE and select column_name from information_schema.columns where table_name='person';
but I need a more flexible solution that will fit these multicolumn queries.
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
If you wrap your query with the following SQL, it will always return the column names from your query, even if it is an empty query result:
select myQuery.*
from (select 1) as ignoreMe
left join (
select * from myTable where false -- insert your query here
) as myQuery on true
Note: When the results of the subquery are empty, a single row of null values will be returned. If there is data in the subquery it won't affect the output because it creates a cross-product with a single row...and value x 1 = value
Execute following command if the result of your previous query is empty
SHOW columns FROM your-table;
For more details check this.
I'm not sure if it will satisfy you but you can do this
SELECT *, COUNT(*) FROM table;
It will return null values (except last column which you can ignore) if the query is empty and you will be able to access all columns. It's not proper way of doing it and selecting names from INFORMATION_SCHEMA would be much better solution.
Please note that result is aggregated and you need to use GROUP BY to get more results if there are any.
You should ,
Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_SCHEMA='yourdb'
AND TABLE_NAME='yourtablename';

Sql Unknown column error 'where clause'

I want to run a SQL query (or multiple queries) on database of my website in phpMyadmin in the SQL tab it says
SELECT * FROM `shell` WHERE 1
SO I typed in
SELECT * FROM `shell` WHERE 'http://samiul.ru.cx/c0de.php';
but I get an error
1054 - "Unknown column 'http://samiul.ru.cx/c0de.php' in 'where clause'
The shell should be an existing table on your phpMyadmin.
After the WHERE statement you should place your column name that you want to filter. For example your table shell contain a column name shell_name with a character data type. Your sql statement should look like:
SELECT *
FROM shell
WHERE shell_name like 'any_name';
If your not planning to filter your table you can remove the WHERE statement.
Leaving you only the main sql statement:
SELECT * FROM shell;
The general syntax for a select query with where clause is:
SELECT * FROM TABLE_NAME
WHERE COLUMN_NAME = 'PARTICULAR_COLUMN_VALUE';
Where clause is used to filter records that satisfy a particular filter criteria.
For example, You have a table say EMP_TABLE with following structure and records,
EMP_NAME | EMP_NUMBER
NISHA | 3322
GRASHIA | 3696
If you want to fetch all columns of all records that have name as 'NISHA', then your query should be:
SELECT * FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
*Note: * in the above query can also be expanded as SELECT EMP_NAME, EMP_NUMBER where you can explicitly mention all the column names
If you want to fetch only particular column say emp number of employee 'NISHA' then your query can be:
SELECT EMP_NUMBER FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
For more reference on Syntax of WHERE CLAUSE
SELECT * FROM *table* WHERE *column* = *value*
In your case:
SELECT * FROM shell WHERE webpage = 'http://samiul.ru.cx/c0de.php'
Or similar.
The WHERE 1 part is a little confusing.
SELECT * FROM table WHERE 1
is functionally the same as
SELECT * FROM table
Here's an explanation: Importance of WHERE 1 in MySQL queries
I've never actually had a need for it, but maybe someone has.
This is a pretty basic question. You might want to read up a little on how queries work.

SELECT in mysql using column number instead of name

Is there any way to do something like :
SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;
?
No, you can't. Column order doesn't really matter in MySQL. See the below question for more details.
mysql - selecting values from a table given column number
If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.
I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.
We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.
How I did it:
I'm trying to take (last 3 values of) column number 4 in sometable.
set #mydb=(SELECT DATABASE());
set #mycol=(select COLUMN_NAME from information_schema.columns where
table_schema=#mydb and table_name='sometable' and ordinal_position = 4);
SELECT Date,#mycol FROM sometable ORDER BY Date DESC LIMIT 3;
Of course, if Database name is known, first line could by whiped and #mydb replaced by real database name.
You can do this trick
Example:
$query="select * from employee";
$result=mysql_query($query);
$meta=mysql_fetch_field($result,0) // 0 is first field in table , 1 is second one ,,, etc
$theNameofFirstField=$meta->name; // this well return first field name in table
// now you can use it in other query
$seconQuery="select $theNameofFirstField from employee";

Pass number as a column name in select statement of Sql

My query is like this
select 5 from mytable_name;
Then the output is like column name 5 and the value is 5 printing as many max number of rows exists in that table.
Can anybody tell the reason why this query is working like this?
Can anybody tell the reason why this query is working like this?
You are selecting a string literal value '5' for each row in your table:
select 5 from mytable_name;
And this works fine. Because in the SELECT statement you can select:
Column reference,
Literal value like in your case.
Function.
value expression.
Select expression.
As defined by the standard SQL1:
Update:
However, If you have a column with a name is a number like in your case, you have to escape it in order to select the values in it like so:
SELECT `143` FROM Table1;
This will select all the rows in the column 143.
But, this:
SELECT 143 FROM Table1;
Will select the string literal 143 for each row found in the table.
Note that: If possible, try not to name these columns this way, it is recommended and a best practice, not to do this.
SQL Fiddle Demo
Update 2:
Note that, if you select 143 or '143', or even "143" this will select the literal value 143 not the column date. The following are the same:
SELECT 143 FROM Table1;
SELECT '143' FROM Table1;
SELECT "143" FROM Table1;
All these SELECTs won't select the data in the column, They will select the literal value 143 not the column data. See this demo:
Demo
You have to escape the column name with the two:
``
Like this:
SELECT `143` FROM table1;
Not:
SELECT '143' FROM table1'
Like what I did here:
The right Demo
1Image From: SQL Queries for Mere Mortals
from mytable
will select all rows from your table if there is no where condition that shrinks that result. and
select 5
will select the constant number 5 for every record. If you use a column name in the select part then that value will be selected for every record.
The DB engine will name the result 5 because it automatically generates a column name and 5 is the logical name for that.
You want 'SELECT * FROM mytable_name LIMIT 0,5' perhaps?
Since you don't have anything in your where clause, it is selecting all the rows from your table. The fact that you don't select any of the columns is irrelevant - you'll still get a result for each row in the table.
With the command select 5 ... you are viewing a fixed value. Same thing you run the following command: select "test", you will be displaying a fixed string.
Using ... from mytable_name you're looking for all record of this table.
With this we can conclude that for each record in the table mytable_name shows you the fixed value "5".