Php count number of how many changed = y where humen=yes - php

I tried something like this:
$changed = mysql_query("SELECT * FROM games WHERE changed = 'y' AND human = '$_GET[human]' ORDER BY id DESC LIMIT 100", $link);
$num_rowsc = mysql_num_rows($changed);
So what i want is to select last 100 where human = yes and count where changed = y ..
so get last 100 humans and count how many of them have on changed yes

Are you looking for something like this :
$stmt = $pdo->prepare("SELECT COUNT(*) AS cnt FROM ( SELECT * FROM games
WHERE human= :human ORDER BY id DESC LIMIT 100
) WHERE changed = 'y' ");
$stmt->bindParam(':human', $_GET[human]);
$stmt->execute();

Maybe with this query?
SELECT COUNT(*) AS cnt FROM (
SELECT * FROM games
WHERE human = '".mysql_real_escape_string($_GET[human])."'
ORDER BY id DESC LIMIT 100
) tmp WHERE changed = 'y'

1) try using this as your query:
SELECT count(*) FROM (SELECT * FROM games WHERE changed = 'y' ORDER BY id DESC LIMIT 100) WHERE human = '$_GET[human]'
2) use mysqli

select count(*) as cnt from (
SELECT human FROM games WHERE human = '$_GET[human]' ORDER BY id DESC LIMIT 100
) as changes WHERE changed = 'y'

Related

PHP: MYSQLI Change Order By Style

Hi, i want to get data from chat table like this:
6
7
8
My Code show like this:
8
7
6
Code:
"SELECT * FROM `chat` WHERE
`chat-code` = 'vm1mxo3dpi9gzuo' AND (`user_1` = '1' OR `user_2` = '1')
ORDER BY id DESC LIMIT 3"
Because you're ordering your result in descending order. You should remove the desc in ORDER BY. You can explicitly put ASC to indicate the order as ascending.
Try to read what will happens if you use DESC or ASC statements. https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
Hope it will help you.
use this mat and you can change the between parameters with prepared statements.comment if doesn't work.
"SELECT * FROM `chat` WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND (`user_1`='1' OR `user_2` = '1')
AND `id` between 6 and 10 //starting and ending offset for query so query doesn't take long time
ORDER BY id ASC LIMIT 3" // LIMIT 3 as you mentioned and ASC order
SELECT a.*
FROM
( SELECT *
FROM chat
WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND 1 IN(user_1,user_2)
ORDER
BY id DESC
LIMIT 3
) a
ORDER
BY id;
I have the solution of your problem - use below query:
SELECT * FROM (
SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo' AND
(`user_1` = '1' OR `user_2` = '1')
ORDER BY `text` DESC LIMIT 3
) t ORDER BY `text` ASC
SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND (`user_1` = '1' OR `user_2` = '1')
ORDER BY id LIMIT 3 OFFSET 5
Try above query.
You can simply change your query to this (order by text ASC):
SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo' AND
(`user_1` = '1' OR `user_2` = '1')
ORDER BY text ASC LIMIT 3
If you still want to order by ID, simply change the DESC to ASC

Select only 5 random rows in the last 50 entries

I'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you
The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;

Order by result of a query / Mysql Subquery

Hi there (I'm new to PHP),
I can't quite figure out the syntax of a subquery I'm trying to make, this is the query:
SELECT * FROM show_episode, shows, show_episode_airdate, show_moyenne
WHERE season = 1 AND episode = 1
AND shows.imdb_id = show_episode.imdb_id_show
AND show_episode_airdate.episode_id = show_episode.episode_id
AND show_moyenne.show_id = shows.id
AND show_episode_airdate.airdate < '2013-07-12'
ORDER BY show_episode_airdate.airdate DESC LIMIT 10
Once this was done, I wanted to order those 10 selected rows by show_moyenne.moyenne with something like that:
SELECT * (FROM show_episode, shows, show_episode_airdate, show_moyenne
WHERE season = 1 AND episode = 1
AND shows.imdb_id = show_episode.imdb_id_show
AND show_episode_airdate.episode_id = show_episode.episode_id
AND show_moyenne.show_id = shows.id
AND show_episode_airdate.airdate < '2013-07-12'
ORDER BY show_episode_airdate.airdate DESC LIMIT 10)
* ORDER BY show_moyenne.moyenne DESC
Which is not correct, anybody can show me the right way to do this ?
Thanks, any help appreciated!
Select * from
(SELECT * FROM show_episode, shows, show_episode_airdate, show_moyenne
WHERE season = 1 AND episode = 1
AND shows.imdb_id = show_episode.imdb_id_show
AND show_episode_airdate.episode_id = show_episode.episode_id
AND show_moyenne.show_id = shows.id
AND show_episode_airdate.airdate < '2013-07-12'
ORDER BY show_episode_airdate.airdate DESC LIMIT 10) as j
order by j.moyenne DESC
I hope this can be of some help.
SELECT x.*
FROM
( SELECT *
FROM show_episode e
JOIN shows s
ON s.imdb_id = e.imdb_id_show
JOIN show_episode_airdate a
ON a.episode_id = e.episode_id
JOIN show_moyenne m
ON m.show_id = s.id
WHERE season = 1
AND episode = 1
AND a.airdate < '2013-07-12'
ORDER
BY a.airdate DESC
LIMIT 10
) x
ORDER
BY moyenne;

SQL query with IN clause

I am working on a page, thats like facebook feed page.
So, my query now look like that:
$sql = "SELECT *
FROM event
WHERE id_user IN (
SELECT a.id_user_stalkers
FROM stalkers a
WHERE a.id_user = ".$id_profile.")
ORDER by date DESC
LIMIT 0, 10";
stalkers - friends
Table Stalkers:
id id_user id_user_friend
I need to append to that IN clause my personal ID, so my qyery returns my events and my friends event.
Can anybody help me ?
What I have tried
$sql = "SELECT *
FROM event
WHERE id_user IN (
SELECT a.id_user_stalkers
FROM stalkers a
WHERE a.id_user = ".$id_profile.")
OR id_user = ".$id_profile."
ORDER by date DESC
LIMIT 0, 10";
it looks something wrong in your sql and your table
1-in your table you have id_user_friend and in your sql you are making id_user_stalkers.
2- i dont know if you have date in your first table , or you mean NOW() .? since u didnt share the first table.
try this
$sql = "SELECT * FROM event e WHERE id_user
IN (SELECT a.id_user_friend FROM stalkers a
WHERE a.id_user = ".$id_profile.")
OR e.id_user = ".$id_user."
ORDER by e.date DESC LIMIT 0, 10";
What is $id_user ? The original query only has one variable. I don't see why you need another. In other words, try this:
SELECT *
FROM event e
WHERE id_user IN (SELECT a.id_user_friend
FROM stalkers a
WHERE a.id_user = ".$id_profile."
) OR
e.id_user = ".$id_profile."
ORDER by e.date DESC LIMIT 0, 10

MYSQL Select from table, get newest/last 10 rows in table

What's the best, and easiest way to do this? My query currently is:
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id
LIMIT 10
This shows the first 10 rows though, not the last 10.
EDIT: I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order.
to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC
EDIT
Based on your comment:
SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC
If you want the last 10 then just change ASC to DESC
SELECT *
FROM
chat
WHERE
(userID=$session AND toID=$friendID)
OR
(userID=$friendID AND toID=$session)
ORDER BY id
DESC
LIMIT 10
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$limit = 10;
$query = "SELECT * FROM $table";
$resource = mysqli_query($con,$query);
$total_rows = mysqli_num_rows($resource);
$start = $total_rows-$limit;
$query_limit= $query." LIMIT $start,$limit";
First I have set the limit
$limit = 10;
then
$total_rows = mysqli_num_rows($resource);
Here I have taken total number of rows affected.
$start = $total_rows-$limit;
then substracted limit from number of rows to take starting record number
$query_limit= $query." LIMIT $start,$limit";
and then added limit to the query.
For more information about limit see this link
https://www.w3schools.com/php/php_mysql_select_limit.asp
First select the last 10 from the table, then re-order them in ascending order.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 10) sub ORDER BY id ASC

Categories