View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 10:43 AM
Captain Paralytic
 
Posts: n/a
Default Re: select syntax with two tables

On 1 May, 01:22, lichu <lisek...@wp.pl> wrote:
> I have a problem with a syntax of "select" with two tables. i have two
> tables:
>
> create table users1
> (
> id_user1 int unsigned not null auto_increment primary key,
> login char(30) not null,
> pass char(40) not null,
> e_mail char(70) not null
> etc...
> );
>
> create table users2
> (
> id_user2 int unsigned not null auto_increment primary key,
> login char(30) not null,
> pass char(40) not null,
> etc....
> );
>
> and when someone want to log into my site I have to check his login
> and password so I need some syntax to sum together this tables and
> then check if login and password of this guy is in this sum (I nedd to
> do this in one syntax). so i try to do this in this way:
>
> select login, pass from (select login,pass from users1) union (select
> login,pass from users2) where login="some_guy" and
> pass=sha1("some_password");
>
> but it doesnt work
>
> only
>
> (select login,pass from users1) union (select login,pass from users2)
>
> works and display content of this two tables together.
>
> I dont know how to do this. enybody knows?? help :-)
> select login,


A little pointer: "it doesnt (SIC) work" is not a very helpful
statement. If you want help, tell us what does happen and what you
expected to happen.

Now, as I see it, you want to find out whether your user credentials
exist in either of the tables. so why not just do:
SELECT
1 as `ok`
FROM `users1`
WHERE `login`="some_guy" AND `pass`=sha1("some_password");
UNION
SELECT
1
FROM `users2`
WHERE `login`="some_guy" AND `pass`=sha1("some_password");

If it returns any rows at all then your user credentials have been
found.

Reply With Quote