SQL SELECT return

0

I need to do a select comparing whether user login is already existent or not. But I do not want it to return me values from the table, just a symbolic value to check for existence (true or false). The comparison code is the basics:

SELECT * FROM USUARIOS WHERE LOGIN = '$LOGIN'

$ LOGIN being the login I'm going to compare. And select will return all table contents unnecessarily.

    
asked by anonymous 24.07.2014 / 15:55

3 answers

3

You could do this:

SELECT count(1) FROM USUARIOS WHERE LOGIN = '$LOGIN'

select will return 1 if it finds the user and 0 if it does not find it. Similar to true and false     

24.07.2014 / 16:01
1

Douglas, try this:

SELECT 1 FROM USUARIOS WHERE LOGIN = '$LOGIN'

SELECT 1 it 1 If the registry exists instead of the table fields, it is very often used in this case that you need to check for a record. And if it does not exist, it returns nothing.

The Electus solution is good, but you will have to do if to check if the return was 0 or 1 and it also uses the COUNT function that will tell you that you will always have only 1 record

    

24.07.2014 / 16:00
0

You can do this in several ways:

select id from usuarios where login = '$LOGIN';

select count(*) from usuarios where login = '$LOGIN' limit 1;

SqlFiddle

    
24.07.2014 / 16:03