mysql case sensitive query for fieldnames - mysql

I would like the query to select only if there is a column named firstName. If the column is firstname then the query should not return anything.
SELECT firstName as currentName from tableNew WHERE id = $Id
I tried the code below but does not work
SELECT binary firstName as currentName from tableNew WHERE id = $Id
Can you please help?

On Unix, table names are case sensitive. On Windows, they are not.
Column, index, and stored routine names are not case sensitive on any
platform, nor are column aliases.

If you have access to INFORMATION_SCHEMA do this:
SELECT firstName as currentName from tableNew WHERE id = $Id AND (SELECT COUNT(TABLE_NAME)
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'mydbname'
AND TABLE_NAME = 'tableNew'
AND BINARY COLUMN_NAME = 'firstName') > 0;

Related

Select from database where string = same string with diacritics

How can I select from database by given string and mysql to return the row on same string with diacritics?
select * from myTable where nume = "Stefan"
and the row from database that should be returned:
id = 1
name = "Ștefan"
You can search like:
SELECT * from myTable
where
CONVERT(nume USING utf8) LIKE '%Stefan%'
But this is a bit unclear, as MySql already know how to search for strings with diactritics.

Select column names with their maximum data length MySQL

I have a table in mysql with many columns and I want to see maximum length of values. My purpose is that I do know that some of data is truncated when insert and I want to increase varchar length. But do not know, what columns. (Explanation little messy, but probably sql will make sense)
I tried:
select COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH, DATA_TYPE, (SELECT LENGTH(COLUMN_NAME) as maxlen FROM my_database.my_table ORDER BY maxlen DESC LIMIT 1)
from information_schema.columns
where table_schema = 'my_database' AND
table_name = 'my_table'AND DATA_TYPE = 'varchar'
It works, but return the length of the column, but not data inside it. (I.e. id column is called id, I got 2).
If I use JOIN (ON TRUE condition), I got error that COLUMN_NAME is undefined.
Stored procedures does not allow for data return, and function does not allow dynamic sql inside it.
How to tell MySQL (in case of my query) to consider COLUMN_NAME not as a string, but as column name? If this is not, possible in select, how to get columns with maximum data inside them?
Desired result looks like:
column_1 | 25 | varchar | 20
column_2 | 25 | varchar | 7
I am interested only in varchar, as int does not make sense to adjust (and no need to). Columns has different length (varchar(20),varchar(25), etc.).
Update 1: This cannot be done also via loop (statements cannot be executed inside cursor).
I use something like this type of code to generate my view automticaly using table schema. Use can modify according to your need.
$sql = "show tables from DBName where Tables_in_yourtbalename = 'yourtbalename' ";
$result = executeQuery($sql, $conn);
$num = $result->num_rows;
if ($num) {
$sql = "show columns from yourtbalename where Extra != 'auto_increment'";
$result = executeQuery($sql, $conn);
$num2 = $result->num_rows;
while ($r = $result->fetch_assoc()) {
if ($r['Key'] == 'MUL' && ( preg_match("/^int/", $r['Type']) || preg_match("/^smallint/", $r['Type']) || preg_match("/^tinyint/", $r['Type']) || preg_match("/^bigint/", $r['Type']))) {
} else if ($r['Field'] == 'status') {
}
}
Where $r['Field'] is field name and $r['Type'] provides its type. For determining maxlength use
$maxlength="' . substr(str_replace(")", "", $r['Type']), 8, (strlen(str_replace(")", "", $r['Type']))));

How get the columnames from select query in MySQL?

Is it possible to get the column names using a query in MySQL? For example, I have a SELECT query:
SELECT name AS 'name', surname AS 'col1', last_name AS 'col2' FROM tb_people;
So, I want to get "Only with one SQL query" like:
Columns or alias of my query
--------
name
col1
col2
I try something similar to:
SHOW COLUMNS (SELECT name AS 'name', surname AS 'col1', last_name AS 'col2' FROM tb_people)
And with
DESCRIBE (SELECT name AS 'name', surname AS 'col1', last_name AS 'col2' FROM tb_people)
But these return SQL errors.
Why don't you do this in your application? MySql API in most languages provide functions to fetch all sorts of field information including name from the results returned by MySql. For example in PHP you can use mysql_fetch_field() on the results returned by mysql_query() to fetch field information.
This is how it is done in PHP, there must be equivalent functions in the language you are using:
$res = mysql_query( "SELECT name AS 'name', surname AS 'col1', last_name AS 'col2' FROM tb_people" );
for( $i = 0; $i < mysql_num_fields( $res ); ++$i ) {
$fieldInfo = mysql_fetch_field( $res, $i );
echo $fieldInfo[ 'name' ].'<br />';
}
results:
name
col1
col2
Hey I have used this successfully in the past, but I don't know if it compatible with all versions:
SELECT name FROM syscolumns
WHERE id = (SELECT id FROM sysobjects
WHERE name= 'MyTableName')
ORDER by colorder
If you installed MySQL server 5, you may get around this problem easily:
use information_schema
SELECT * FROM information_schema.`COLUMNS` C limit 5
WHERE table_name = 'your_table_name'
Cheers!

mysql distinct select exclude it self query

how to return string exclude query string it self.
column string = AAA/BBB/CCC
result string = BBB/CCC where column like AAA
column string = AAA/BBB/CCC
result string = CCC where column like AAA/BBB
"SELECT DISTINCT `column` FROM `table` WHERE `column` like '???'";
Thank you.
select distinct replace(column, 'AAA/BBB/', '') as column
from table
where column like 'AAA/BBB/%';

How to return field type from MySQL query?

Simple question: How can I return the field type of a MySQL table. I know about describe or show column but I just want to return that single parameter. e.g.:
SELECT fieldtype(mycol) FROM mytable
# should return INT or integer for example
You can use
SHOW FIELDS
FROM tableName where Field ='nameOfField'
This will return you result in format of
Field Type Null Key Default Extra
You can get this from the information_schema database:
select data_type
from information_schema.columns
where table_schema = 'myschema'
and table_name = 'mytable'
and column_name = 'mycol'
I know I'm a little late to the party. You may want to use COLUMN_TYPE rather than DATA_TYPE. It gives more info (such as precision) about the type.
SELECT COLUMN_TYPE
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'parts'
AND COLUMN_NAME = 'price'
...yields...
decimal(11,2)
The following syntax also works, using describe, enter the columnName after the tableName:
describe tbleName columnName;
ResultSet rs = Sstatement.executeQuery("SELECT * FROM Table Name");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnTypeName(i));
}