MySQL QUERY to get row where column ellipse number >= 1 - php

Posts
-------------
5
4
6
7
3
I want to get the rows where Posts - 4 >= 1
that mean i wanna get
QUERY Result :
5
6
7
Something like that : SELECT content FROM mytable WHERE (Posts - 4) >= 1 LIMIT 10
Thanks

So then you could use:
SELECT content FROM mytable WHERE Posts >= (1 + "yournumber") LIMIT 10;

You want something like this (if I'm reading you want to take the value in your column and subtract some number from it, then narrow the results which are 1 or greater).
SELECT content FROM mytable WHERE mycolumn-4 >= 1

Since you are doing this with php, create a variable to hold your number, create a query parameter from it, and send the paramter to your query.

maybe something like this?
SELECT content FROM mytable WHERE Post >= (SELECT COUNT(*) FROM mytable) LIMIT 10

Related

How count sum of field value and sort them as per userid using PHP and MySQL

I need some help. I need to calculate the sum of numeric field value as per userid and place them into array using MySQL and PHP. I am explaining my table below.
db_like:
id like userid
1 1 10
2 2 11
3 1 10
4 2 11
Here I need to count the sum of like field value as per userid and place the result into array. Please help.
SELECT SUM(`like`) AS `likes` FROM [table] GROUP BY `userid`;

Lowest free value in mysql column

I already searched but I always find LEAST and GREATEST as hints. I want to have the next ascending number in a row that's not used. Like the following:
entries
1
2
3
5
6
7
If every of the numbers is for one row in my table I want the number 4 as a result and in the following example:
1
2
3
4
5
6
I want the number 7 as a result. Is there any possiblity to accomplish this in an SQL statement?
Best,
Robin
This query assumes that the number 1 is in your table
select min(number) + 1 from entries e1
where not exists (
select 1 from entries e2
where e2.number = e1.number + 1
)
If you want all missing numbers (where gaps are no larger than 1) instead of the smallest one, then remove min()
It think the solution is to do a self-join with the next value, and extract the first lowest result. Example:
Table: values, with column value
SELECT v1.value
FROM values v1
LEFT JOIN values v2 ON v1.value = (v2.value + 1)
WHERE v2.value IS NULL
ORDER BY v1.value ASC
LIMIT 1

mysql - getting only the results with diferences from same table

So I have a single table inside which I have a score system for points. It looks something along this line:
Columns:
ID Name Date Points
1 Peter 2014-07-15 5
2 John 2014-07-15 6
3 Bill 2014-07-15 3
and so on...
Everyday, the new results are being put into the table with the total amount of points acumulated, however in order to be able to get historic values, the results are put into new rows. So on the 2014-07-16, the table will look like this:
ID Name Date Points
1 Peter 2014-07-15 5
2 John 2014-07-15 6
3 Bill 2014-07-15 3
4 Peter 2014-07-16 11
5 John 2014-07-16 12
6 Bill 2014-07-16 3
However sometimes when a player doesn't take part for the whole day and doesn't get any points, he will still be added, but the points will remain the same (here this is shown by the case of Bill).
My question is how to count the number of each type of players (active - Peter and John ie when the points value changes from one date to another and inactive - Bill ie when the points value stays the same).
I have managed to get this query to only select players who do have the same value, but it's giving me the list of players rather than the count. Although I could potentialy be wrong with this query:
SELECT Points, name, COUNT(*)
FROM points
WHERE DATE(Date) = '2014-07-15' OR DATE(Date) = '2014-07-16'
GROUP BY Points
HAVING COUNT(*)>1
I'm not sure how to count the number of rows (could do a bypass trick with PHP getting the number of rows, but interested in SQL only) or how to invert it, to get a count of players who have a different score (again, could get total of rows and then subtract the above number, but not interested in that either - I'd prefer the SQL).
Regards and thanks in advance.
You are pretty close.
If you have at most one row per "player" per "date", you could do something like this:
SELECT SUM(IF(c.cnt_distinct_points<2,1,0)) AS cnt_inactive
, SUM(IF(c.cnt_distinct_points>1,1,0)) AS cnt_active
FROM ( SELECT p.name
, COUNT(DISTINCT p.points) AS cnt_distinct_points
FROM points p
WHERE DATE(p.Date) IN ('2014-07-15','2014-07-16')
GROUP BY p.name
) c
The inline view query (aliased as c) gets a count of the distinct number of "points" values for each player. We need to "group by" name, so we can get a distinct list of players, along with an indication whether the points value was different or not. If all of the non-NULL "points" values for a given player are the same, COUNT(DISTINCT ) will return a value of 1. Otherwise, we'll get a value larger than 1.
The outer query processes that list, collapsing all of the rows into a single row. The "trick" is to use expressions in the SELECT list that return 1 or 0, depending on whether the player is "inactive", and perform a SUM aggregate on that. Do the same thing, but a different expression to return a 1 if the player is "active".
If the count of distinct points for a player is 1, we'll essentially be adding 1 to cnt_inactive. Similarly, of the distinct points for a player is greater than 1, we'll be adding 1 to the cnt_active.
If this doesn't make sense, let me know if you have questions.
NOTE: Ideally, we'd avoid using the DATE() function around the p.Date column reference, so we could enable an appropriate index.
If the Date column is defined as (MySQL datatype) DATE, then the DATE() function is unnecessary. If the Date column is defined as (MySQL datatype) DATETIME or TIMESTAMP, we could use an equivalent predicate:
WHERE p.Date >= '2014-07-15' AND p.Date < '2014-07-16' + INTERVAL 1 DAY
That looks more complicated, but a predicate of that form is sargable (i.e. MySQL can use an index range scan to satisfy it, rather than having to look at every row in the table.)
For performance, we'd probably benefit from an index with leading columns of name and date
... ON points (`name`,`date`)
(MySQL may be able to avoid a "Using filesort" operation for the GROUP BY).
I would solve this problem by looking at the previous number of points and then doing a comparison:
select date(date), count(*) as NumActives;
from (select p.*,
(select p2.points
from points p2
where p2.name = p.name and p2.date < p.date
order by p2.date desc
limit 1
) as prev_points
from points p
) p
where prev_points is NULL or prev_points <> points;
Of course, you can add a where clause to get the count for any particular day.

Getting the last result using LIMIT in sql

I have created a table that is similar to the one below, my goal is to LIMIT the result to 10 AND THEN return the id of the LAST result which is 10. I've tried doing, the query below but it keeps returning my the value of 15, instead of 10.
SELECT id FROM this_table WHERE value=value ORDER BY id DESC LIMIT 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
select max(id) from
(
SELECT id
FROM this_table
WHERE value = 'some_value'
ORDER BY id
LIMIT 10
) x
LIMIT can take two parameters.
Try
SELECT id FROM this_table WHERE value=value
ORDER BY id LIMIT 9,1
Read all about it.
EDIT: Oh, and loose the DESC part. It seems you don't really need it.

I want to ascend and decend MYSQL from a value in the database

I want to ascend MYSQL from a value in the database.
Hypothetically:
database with table tbl_numbers column numbers has 10 values in it 1-10.
I want to:
order by numbers desc {from 8} LIMIT 3
it would show
8
7
6
instead of:
order by numbers desc LIMIT 3
10
9
8
If this is possible what php and not mysql that's good too.
--update!--
The example I gave was way to easy, I really need to match the date() with the
dates of the entry's in the database and start the asend or decend from there any ideas? The date format is in 2010-06-18
ORDER BY numbers DESC
LIMIT 3,3
First argument for limit is (10 entries + 1) - 3;
second argument is the number of records to return
Alternatively:
WHERE numbers <= 8
ORDER BY numbers DESC
LIMIT 3
Something along the lines of
ORDER BY (numbers < 8) DESC, numbers DESC LIMIT 3
should do the job.

Categories