Retrieving 1 record from 2 tables with where syntaxBy Bill Graziano on 5 September 2000 | 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 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 * 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
|
- Advertisement - |