slier
slier

Reputation: 6750

Determine table existence

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.

Upvotes: 1

Views: 52

Answers (3)

Vyktor
Vyktor

Reputation: 21007

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.

Upvotes: 2

user725913
user725913

Reputation:

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

Upvotes: 0

AngryHacker
AngryHacker

Reputation: 61646

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>

Upvotes: 0

Related Questions