Getting next results - php

I have a table that is something like the one below.
id | episode_number
1 55
2 56
3 57-58
4 59
5 60
6 61-62
7 63
8 64
9 65-66
10 67-68
How would I get the next 5 episodes after 57-58 without specificity the id, just the episode_number such as episode_number > 57-58 LIMIT 5.
But obviously episode_number > 57-58 would not work since it contains string in it. The database will just consider it as 5 and return the next 5 results after the episode_number 5.

Use CAST function to convert string to number, then your query:
SELECT episode_number
FROM table_name
WHERE CAST(episode_number AS UNSIGNED) > CAST('57-58' AS UNSIGNED)
ORDER BY CAST(episode_number AS UNSIGNED)
LIMIT 5
If there are two numbers, for example: '57-58', then CAST('57-58' AS UNSIGNED) converts and returns first one: 57

Try
SELECT TOP 5 * FROM tablename WHERE id >= (SELECT id FROM tablename WHERE episode_number = "57-58")
or if it's MySQL
SELECT * FROM tablename WHERE id >= (SELECT id FROM tablename WHERE episode_number = "57-58") LIMIT 5

You can select like this:
select id , case when LOCATE('-' , episode_number) = 0 then episode_number
else LEFT(episode_number , LOCATE('-' , episode_number)) end
as start_episode ,
case when LOCATE('-' , episode_number) = 0 then episode_number
else RIGHT(episode_number , LEN(episode_number) -LOCATE('-' , episode_number)) end
as end_episode
this should give you the base structure for such queries, you should consider converting the values into numbers to be able to easily compare them.
If possible restructure the table to something that has the start episode and end episode in separated fields

Related

Number of 1 time,2 times, 3 times, n times column entries in table in php mysql

Have Table :
id userid type created_date
1 4353535 1 04-06-2014
2 4353536 0 06-06-2014
3 4353537 1 11-06-2014
4 4353538 1 11-06-2014
5 4353539 0 19-06-2014
7 4353541 1 01-06-2014
10 4353544 1 12-06-2014
11 4353535 1 06-06-2014
12 4353536 1 10-06-2014
13 4353537 1 12-06-2014
What I Want : (with in date range)
How much user have single time entry with type 1
How much user have double time entry with type 1
How much user have triple time entry with type 1
How much user have four time entry with type 1
How much user have n time entry with type 1
(PHP & MYSQL)
First get the count for each user, then from that group the entrycount you can get your expected output
select EntryCount, count(userid) from (Select userid, count(id) as Entrycount from myentry group by userid where type=1) as sq group by Entrycount
This will work try
First, get the number of entries per user. Then, get the number of users grouped by number of entries.
SELECT numEntries, COUNT(*) AS numUsers
FROM (
SELECT userid, COUNT(*) AS numEntries
FROM tablename
WHERE type = 1
GROUP BY userid
) tbl
GROUP BY numEntries
Simplified demo
Demo with your data

Getting the sum of the column in mysql

I am using user Keevas' example, but asking a different question....
select Master_Code, SUM(Jan), SUM(Feb), SUM(Mar)
from dbo.foobar
WHERE Participating_City = 'foofoo'
GROUP BY Master_Code ORDER BY Master_Code ASC
something like this:
Master_Code sum(Jan) Sum(Feb) sum(Mar) Total
1 4 5 4 13
2 5 5 5 15
How do I get Total value column?
SELECT username,SUM(value) AS SumValue FROM
table GROUP BY username ORDER BY SumValue DESC

How can I select all the values of a database and choose one of them by a percent

What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid

MySQL: Getting value from column A that corresponds to MIN from column B

I have the following table:
step_id hospital_id step_number step_name
17 8 4 First Step
18 8 1 Second Step
19 8 2 Third Step
20 8 3 Finance Approval
What I am trying to do is get the step_id that corresponds to the smallest step number. So in the example above I am looking for step_id 18.
After looking over many posts I have been trying variations of the following to no avail:
SELECT `step_id`
FROM `progress_steps`
WHERE `hospital_id` = 8
GROUP BY `step_id`
HAVING MIN(`step_number`)
It seems to have worked for other but the above returns all rows from the example and other variations I have tried give me step_id 17 only.
You can do that by using ORDER BY and LIMIT
SELECT *
FROM tableName
WHERE hospital_id = 8
ORDER BY step_number ASC
LIMIT 1
SQLFiddle Demo
or if you want to get multiple rows having the same lowest step_number, use subquery:
SELECT *
FROM tableName
WHERE step_Number =
(
SELECT step_Number
FROM tableName
WHERE hospital_id = 8
ORDER BY step_number ASC
LIMIT 1
)
SQLFiddle Demo
SQLFiddle Demo (with duplicate record)
Try this one
SELECT `step_id`
FROM `progress_steps`
WHERE `hospital_id` = 8
Order BY `step_id`
LIMIT 1

PHP - SQL: fetching results in round robin fashion

I have a table, which consists of 3 fields:
id
name
status
Every time I get the results, it should give me 5 names whose status = 1.
Suppose the db contains following:
id name status
1 A 1
2 B 1
3 C 0
4 D 1
5 E 0
6 F 0
7 H 1
8 I 1
9 J 1
10 K 1
11 L 1
12 M 0
1st time, fetch should return: A,B,D,H,I (5 records)
2nd time, fetch should return: J,K,L,A,B (5 records)
UPDATE: I don't want typical pagenation. Consider I have 12 available names from A1 to A12. The first fetch should return A1-A5, second fetch A6-A10 and third fetch A11, A12, A1, A2, A3. So when I reach the end, I need to get records starting from the first to fill the 5 slots.
i am doing it in php with mysql
This looks like some sort of job allocation script?
You need 2 things:
the highest ID returned last time the script was run (lastID)
a number larger than the maximum ID in the table (bigNum)
Then you can write your query as
SELECT
id, name
FROM
table
WHERE
status=1
ORDER BY
(bignum + id) MOD (bigNum + lastID + 1)
LIMIT 5
Shazaam!
Keep track of the ids of the records returned, and for the following queries do:
select top 5 *
from (
select top 5 *
from MyTable
where status = 1
and id not in (1,2,4,7,8)
order by name
union
select top 5 *
from MyTable
where status = 1
order by name
) a
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5);
while ($row = mysql_fetch_row($q))
{
.... //first 5
}
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5,5);
while ($row = mysql_fetch_row($q))
{
.... //second 5
}
this uses the offset functionality of mysql- think of it as pagination for your results.

Categories