select syntax with two tables

This is a discussion on select syntax with two tables within the MySQL Database forums, part of the Database Forums category; I have a problem with a syntax of "select" with two tables. i have two tables: create table ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-01-2007
lichu
 
Posts: n/a
Default select syntax with two tables

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,

Reply With Quote
  #2 (permalink)  
Old 05-01-2007
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
  #3 (permalink)  
Old 05-16-2007
lichu
 
Posts: n/a
Default Re: select syntax with two tables

Of curse!!
heh I didn't know that it is so simple :-)

thanks very much for answer.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:38 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0