Jump to content

Limit by ID !


egturnkey

Recommended Posts

Hello friends,

 

if i've database table (mytable) has the following ids

1

2

3

4

5

6

 

and i want to get it with limit the first (1 to 3 ) only

 

$sql ="select *  from  mytable LIMIT 3"; 

this will show the first 3 (1,2,3)

 

how then i write code that shows which is after 3

so it shows me

4

5

6

 

and if there any way i can say

$sql ="select *  from  mytable LIMIT (first half of ids)"; 

and (shows 1,2,3..ect till half)

$sql ="select *  from  mytable LIMIT (second half of ids)"; 

will (shows 4,5,6...ect till end)

 

:shrug:

thank you

Link to comment
Share on other sites

LOL i've an idea ..

 

i may use (Count) to get the total count of ids -->1

then i divied it by 2 to be $half = $count / 2

then i use $half to be

 

$sql ="select *  from  mytable LIMIT 0,$half";

 

and to get the seond half lol

 

$sql ="select *  from  mytable LIMIT $half,$count";

 

lol looks very hurting way  :D

 

is it possible

 

Ops, what if we have odd number of ids  :confused:

Link to comment
Share on other sites

Try an ORDER BY desc (then add the LIMIT again).

 

thank you for your answer but i mean if i've database table with alot of ids ( but i don't know how many )

 

and willing to cut it into 2 parts

part 1 = show from 1 to exact half of the ids

part 2 = show from exact half of the ids to the end

Link to comment
Share on other sites

Ahh.

 

Then yes your suggestion of total divided by two.

You may want to incorporate the modulus for the cases where it's an odd total:

 

http://php.net/manual/en/internals2.opcodes.mod.php

 

thanks for help , that make it more complex lol

 

now with your help i can know if it even number or odd number

so i can use double way

 

if even then will divided by 2

if not even ( sure will be odd ) then i will divided by 2 then sub - 1

 

lol life is so hard

 

so let the total ids

 

$count

 

if ($count % 2) {

$half = $count / 2  // not eved so divied by 2

} else {

$makeiteven = $count - 1 // odd number so  - 1

$half = $makeiteven / 2  // divied by 2 then

}

 

then

 

part 1

 

$sql ="select *  from  mytable LIMIT 0,$half";

 

and

 

part 2

 

$sql ="select *  from  mytable LIMIT $half,$count"; 

 

i wonder is there any way makes life more easy than that way  :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.