Rks Rock
Rks Rock

Reputation: 79

get two person names in a single query from the same table in one row

Description :

I have a table which has a coloumn name 'student_name'.

Now each student has a registration id through which all the actions in the website take place.

Now I am at a point where if one student of same class (as class mate) posts something then the other class mate gets notified ... every thing is working fine but there is a performance issue right now I am doing this

SQL and PHP

$pn = $f_notif['notif_by'];
$rn = $f_notif['notif_of'];
$sql_1 = mysql_query("select stname from students where stregno = '$pn'");
$sql_2 = mysql_query("select stname from students where stregno = '$rn'");

Now $sql_1 holds the name of the poster student and $sql_2 holds the name of the 2nd student who gets notified.

Notice that I am making 2 requests to get the names of these two students which being fetched from different queries require mysql_fetch_array() to run two times aswell

What I want :

what I want is to some how get the two names in 1 sql query so that I am not making 2 simultaneous requests to the server.

Any one ?? I am stuck

Upvotes: 0

Views: 82

Answers (1)

BMN
BMN

Reputation: 8508

You can use IN :

SELECT stregno, stname FROM students WHERE stregno IN ('$pn', '$rn')

Or, a OR clause :

SELECT stregno, stname FROM students WHERE (stregno = '$pn' OR stregno = '$rn')

Upvotes: 4

Related Questions