Re: Displaying results either side of an Query?
| From: | Steve Lawson | Date: | Fri, 13 Oct 2000 07:36:55 +0000 |
| Subject: | Re: Displaying results either side of an Query? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19960@lists.php.net to get a copy of this message | ||
Yo,
Sounds like you need to do a join in your query.
I assume you have some sort of (id) column and you want to display results
from ((your query id) - 15) to ((your query id) +15)?
Here is an example. Hopefully this is what your trying to do... Let's say
you have a mysql table (mytable) with 2 columns (id) and (subject).
$query = "select t1.id as id , t1.subject , t2.id as target from mytable as
t1, mytable t2 where t2.id=30 having t1.id > target -16 and t1.id < target +
16";
$result = mysql_query($query , $db);
That will return a 31 row result with records from 15 to 45 with a result
table that looks something like this:
| id | subject | target |
------------------------------
| 15 | Some Guy | 30 |
| 16 | Another D | 30 |
| 17 | Blah Blah | 30 |
...
You don't even have to know the target id, anything column name can be used
in the where part. Let's say you only knew the subject of the target row.
"...mytable t2 where t2.subject=\"HI!\" having..."
---
I'll try to explain how this works. The query sorta combines 2 selects in
one. When we say "...where t2.column=whatever..." it's just like saying
"select * from mytable where column=whatever". So now, any t2 columns that
are in the select part of the query (in the above case it was just [t2.id as
target]) get the value from the where clause. Then comes the having clause
which can only use columns defined in the select part (t1.id as id ,
t1.subject , t2.id as target). target will be the id from the result of the
where clause, so you can now tell it to give you the t1 values from 15
before target and 15 after target.
That's probably a big mess, hopefully you get something out of it.
Also, you can use * in the select part if you would like.
select t1.* , t2.id as target from mytable as t1, mytable t2 where
t2.subject="whatever" having t1.id > target -16 and t1.id < target + 16
This would return those 31 rows will every column from your table.
SL.
----- Original Message -----
From: "Joseph H Blythe" <joe.blythe@binarylogic.com.au>
To: "PHP-General" <php-general@lists.php.net>
Sent: Wednesday, October 11, 2000 9:17 PM
Subject: [PHP] Displaying results either side of an Query?
> Hey all,
>
> I got a bit of a tricky problem, I need to be able to find a result in a
> mysql table and then dispaly this result in the middle of a predefined
> number of results for example:
>
> 15 results
> *My Query*
> 15 results
>
> Now I have tried a few things but I can't seem to work out how to do
> this, I need to be able to find the row number of My Query, then count
> back 15 use this as the start point with a offset of 15 then add 1 to My
> Queries placment with a limit of 15. Then I would have to combine all
> this together in a while loop to spit it all out in a html table.
>
> Has anyone ever tried this sort of thing before, as this sort of method
> would at least need 3 queries. Is there a better way? How do I find out
> a rows place in a query?
>
> Any help would be greatly appreciated if anyone can make sense if this.
>
> Regards
>
> Joseph
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>