I need to know which is the best way to check for a row:

Without column; only just a number:

SELECT 1 FROM users WHERE users.id = 1 LIMIT 1

With column and database( users.id )

SELECT users.id FROM users WHERE users.id = 1 LIMIT 1

With column and without database

SELECT id FROM users WHERE id = 1 LIMIT 1

With all fields( obviously inefficient ):

SELECT * FROM users WHERE users.id = 1 LIMIT 1

And It is recommended to use the sentence "limit" when the sentence "exists" is used?

Example with limit:

SELECT products.* FROM products
WHERE EXISTS(
    SELECT 1 FROM users WHERE users.id = products.user LIMIT 1
)

Example without limit:

SELECT products.* FROM products
WHERE EXISTS(
    SELECT 1 FROM users WHERE users.id = products.user
)

Thanks!

没有正确的解决方案

其他提示

I would do this:

select count(*) records
from users
where id = 1

Then I would check the value of records in my application.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top