4 ways for MySQL to determine whether there is data

Source: Internet
Author: User

1.SELECT Count (*) from ' feed ' WHERE ' uid ' =1121293

You can return the total number of eligible records

The following method is recommended by me.

SQL statement: Select 1 from tablename where col = col Limit 1;

The number of rows affected by the execution of the statement is then read.

Of course it's important to limit 1 here. This will not be looked down after MySQL finds a record. The number of rows affected by this implementation is either 0 or 1, and performance has improved considerably.

If you're using a PDO, you can use ROWCOUNT () and it's easy to get to the number of rows affected by the execution.

2.SELECT * from ' feed ' WHERE ' uid ' =1121293 limit 1

Can return the field contents of a record

3.SELECT EXISTS (SELECT * from ' feed ' WHERE ' uid ' =1121293)

You can only determine whether a record exists, and the SELECT * in a subquery is ignored when it is executed.

4. Use the procedure to judge


<?php

$sql = "SELECT * from checklist where game_id= $gid and task= ' $task ' and status= ' $status '";
$result =mysql_query ($sql);

$row = Mysql_fetch_array ($result, MYSQL_ASSOC);

if (!mysql_num_rows ($result))
{
echo "Record doesn ' t exist~~~~~!!!!!!";
}
Else
{
echo mysql_num_rows ($result);
echo $row [' game_id '];
echo $row [' Task '];
}
?>

The above three methods generally think the third is the most efficient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.