Retrieving 1 record from 2 tables with where syntax

By Bill Graziano on 5 September 2000 | 2 Comments | Tags: SELECT


jan asks "i need to know if there is a reccord in any of the two tables which has the username and password provided by the form . . "

The full text of the question is "i have 2 tables

+---table1-----+
|id
|password
|username
+--------------+

+---table2-----+
|id
|password
|username
+--------------+
now i have a login page with 2 fields: password and username..
now i need to know if there is a reccord in any of the two tables which has the username and password provided by the form.
Further on i need to get the table from which it came and the id
the id is being stored inside an other database so the id's from the two tables are autonumberd like they would if it was one big table..

i cannot merge the two tables into one because of the differences in information being stored in them..

thanks in advance..
jan"


We discussed using a UNION in a query in an earlier article. We use that with a small variation here. First we'll create a view that will get us the union of the two tables.

CREATE VIEW vUsers AS
SELECT TableName = 'Table1',
id, password, username
FROM Table1
UNION
SELECT TableName = 'Table2',
id, password, username
FROM Table1


Notice that we used the field "TableName" to tell us which table each record is coming from. After that our select statement is pretty darn easy:

SELECT *
FROM vUsers
WHERE Username = 'user'
AND password = 'password'


That should tell you whether you have a matching user and which table it came from. You can also do this in one query by putting the where clause in both SELECTs in the UNION query. Either way.
-graz

Discuss this article: 2 Comments so far. Print this Article.

If you like this article you can sign up for our weekly newsletter. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Related Articles

Joining to the Next Sequential Row (2 April 2008)

Writing Outer Joins in T-SQL (11 February 2008)

How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007)

How to Use GROUP BY in SQL Server (30 July 2007)

SQL Server 2005: Using OVER() with Aggregate Functions (21 May 2007)

Server Side Paging using SQL Server 2005 (4 January 2007)

Using XQuery, New Large DataTypes, and More (9 May 2006)

Counting Parents and Children with Count Distinct (10 January 2006)

Other Recent Forum Posts

Query 2 tables, update one with data from other (4 Replies)

Complex: JOIN with LIKE, but only for NULL results (5 Replies)

Case statement with Group BY (2 Replies)

Move columns into rows for same items (4 Replies)

adding a date seems to remove any null values (4 Replies)

Sum where date is between two dates (5 Replies)

SQL query to understand the names of all the avail (1 Reply)

How to get Microsoft SQL Server 2012 started? (0 Replies)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -