Determine table existence - mysql

I got this piece of code from Mysql 4th edition to check table existence
SELECT * FROM table WHERE FALSE;
I cant quite under stand the where clause.
If im not mistaken there is no boolean type in mysql, so basically it been cast to
SELECT * FROM table WHERE 0;
And should it be a column on a where clause?
SELECT * FROM table WHERE column = false;
Any explaination greatly appreciated.

No it doesn't have to have column as operand :)
When you open mysql select manual than expressions you would find there simple_expr what should mean conditions like WHERE 1 (about booleans) but I understand it can be confusing (and it's rare to use conditions without columns).
When you do SELECT * FROM table WHERE FALSE; it's basically this:
if there's table `table`
return 0 rows (minimal database overhead) and valid resource
else
return false
Just take a look on return values from mysql_query.

I am assuming that MySQL 4.x supports ANSI. If so, you could try the following:
SELECT count(*)
FROM information_schema.tables
WHERE table_schema = <schema-or-db-name>
AND table_name = <table-or-view-name>

Checking on a Table's Existence
$sql="SELECT * FROM $table";
$result=mysql_query($sql);
if (!$result)
{
// table does not exist
}
To answer your question:
The where clause is used to retrieve the value of a column in the given table. For example, if your table consists of two columns (user_id, user_name), your query might look something like the following:
$sql="SELECT * FROM $table where user_id = 1";
Lastly, you may read more about the where clause at this link

Related

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';

MYSQL - how to select a particular column name

This is a really basic mysql question but I rarely work with mysql so here it is.
I understand the basics of selecting particular rows in a table. Here is my statement
"SELECT * FROM realForeclosure WHERE auctionDate BETWEEN '2014-12-08' AND '2014-12-09' "
But this returns all of the columns for the selected rows. For this particular query I only need one column 'AUCTION_ITEM', How can I return only that data for the selection above?
Replace the * by the list of your columns:
SELECT `AUCTION_ITEM` FROM realForeclosure WHERE auctionDate BETWEEN '2014-12-08' AND '2014-12-09'
You could have just found this in the documentation.
"SELECT DISTINCT * FROM realForeclosure WHERE auctionDate BETWEEN '2014-12-08' AND '2014-12-09' "

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";

Is it possible in SQL to SELECT * FROM a table WHERE the column 1 = something and not the column_name = something

I would like to SELECT * FROM table where the first column is equal to a variable. It supposed that I don't know the column name.
I know I can do something like
SELECT * FROM table WHERE column_id = 1
But I can't compare the data.
How can I do that?
I found some solution with T-SQL but it doesn't interest me.
To be more accurate :
I'm developing an administration panel in my website where the "super" admin can directly modify the database. For that I can select a table and edit this table. But to do that, I'm using an only PHP script which showing all tables, we can select one and the script show all rows in the selected table. After that you select a row and you are redirected to a page where the problem is. This page can receive any table with only one row, so I want to SELECT the data contained in this row.
Images to understand:
The first one shows the tables.
The second shows the rows of a selected table.
The third shows (normally) the data of 1 row but in this picture we can see data of many rows.
selecto http://imageshack.us/g/135/selecto.png
I found a solution :
Try to explain:
First : I selected all form the specific table which was posted
$query="SELECT * FROM ".$_POST['table']."";
$result=mysql_query($query);
Second: I attributed to a variable the column name (which I didn't know)
while($fields=mysql_fetch_array($result))
{
$col = mysql_field_name($result,0);
$nb++;
}
Third: I selected data from the table where $col = id of the row
$sql = "SELECT * FROM ".$_POST['table']." WHERE ".$col."=".$_GET['idRow']."";
$result1=mysql_query($sql);
If you know how many columns there are, you could use this little trick here:
SELECT *
FROM (
SELECT null x1, null x2, ..., null xn
WHERE 1 = 0
UNION ALL
SELECT * FROM my_table
) t
WHERE t.x1 = something
In other databases than MySQL, renaming "unknown" columns would be even simpler, e.g. in PostgreSQL you could rename only the first column like this:
SELECT * FROM my_table t(x) WHERE x = something
If you don't know anything about the table
... you can quickly query the information_schema first:
SELECT column_name
FROM information_schema.columns
WHERE table_name = :my_table
AND ordinal_position = 1
A note on SQL injection
Please don't, DON'T do this. EVER:
$query="SELECT * FROM ".$_POST['table']."";
I've recently written an article about SQL injection. Every single vulnerability like yours will allow any script kiddie to dump your database, or worse.
The solution is to sanitize your input first. Ideally, you'll maintain a catalog of allowed table strings, compare your $_POST variable with those, and then concatenate the pre-defined table string into your SQL statement, NOT the user input.
I think you can use SHOW CREATE TABLE table_name to fetch the schema of the table. After that, you should already know every column.
In PHP you could do something like:
$col = 'users';
mysql_query("SELECT * FROM table WHERE $col = $something");

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).