Re: Re: A complex query problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



MySQL version 4.x has Subquery support.


On Fri July 4 2003 5:54 am, Nadim Attari wrote:
> From MS-SQL Server Manual:
> A subquery is a SELECT query that returns a single value and is nested
> inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another
> subquery. A subquery can be used anywhere an expression is allowed.
>
> Unfortunately you do not have Sub-queries in mySQL ...
> Try this script...
>
> <?php
>     $link = mysql_connect('localhost', 'root', '') or die('Could not
> connect');
>     mysql_select_db('test') or die('Could not select database');
>
>     // Declare vars..
>     $sports = $graduation = $both_activities = array();
>
>     // Get all the schools where there is "Sports"
>     $sql = "select distinct sid from school_highlight_details where hid =
> '2'";
>     $result = mysql_query($sql) or die("Query failed");
>     while (list($school_id) = mysql_fetch_row ($result)) $sports[] =
> $school_id;
>     mysql_free_result($result);
>
>     // Get all the schools where there is "Graduation"
>     $sql = "select distinct sid from school_highlight_details where hid =
> '4'";
>     $result = mysql_query($sql) or die("Query failed");
>     while (list($school_id) = mysql_fetch_row ($result)) $graduation[] =
> $school_id;
>     mysql_free_result($result);
>
>     // Find common in both..
>  $both_activities = array_intersect($sports, $graduation);
>  $condition = '(';
>  foreach($both_activities as $anything => $school_id) $condition .=
> "$school_id, ";
>  $condition = substr($condition, 0, -2) . ')';
>
>  $sql = "Select sID, school_name FROM school where sID in $condition";
>  $result = mysql_query($sql) or die("Query failed");
>     while (list($sID, $school_name) = mysql_fetch_row ($result)) echo
> "$sID. $school_name<br>";
>     mysql_free_result($result);
>
>     mysql_close($link);
> ?>
>
> If you are using mySQL 4, read on UNION ...
>
> (SELECT a FROM table_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
> UNION
> (SELECT a FROM table_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10)
> ORDER BY a;


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux