TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   2nd table (http://www.talkphp.com/mysql-databases/4047-2nd-table.html)

Sakakuchi 03-16-2009 04:21 PM

2nd table
 
Hi there,

Is it possible to make an query like:

Table 1: id;nickname(unique);rights
Table 2: id;email;nickname

Now I want to select the email where rights equals to 3.

Is it possible to write that in one single query?

(The tables cannot be put together in one because thats an excisting system...)

delayedinsanity 03-16-2009 05:36 PM

You want to look up what's called a 'join', ie; http://www.w3schools.com/Sql/sql_join.asp

PHP Code:

SELECT t1.idt1.nicknamet2.email FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id=t2.id WHERE t1.rights 

Broken down:

PHP Code:

SELECT t1.idt1.nicknamet2.email
// Select the id, nickname and email data. We don't need to select the rights, we already know
// everybody selected from this query should be equal to 3

FROM table1 AS t1
// The primary table we're operating on

INNER JOIN table2 AS t2
// We use an inner join because we only want to return records where there is at least one
// row in both tables that match the join condition.

ON t1.id=t2.id
// The join condition

WHERE t1.rights 3
// Where the rights column of table one is equal to 3 

I haven't programmed in nearly 6 months so I could be a little rusty but I'm fairly certain this is what you're in the market for.

Sakakuchi 03-17-2009 06:29 AM

Yeah, that should do it. THX alot :-)


All times are GMT. The time now is 10:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0