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
Related
As this question clearly means that, I am not asking about SQL joins here,
I just want to combine/merge 2 MySQL result in PHP, i have tried to do it with PHP array_merge() but no success.
$user_paid_query = "SELECT * from users WHERE now_paid!=0 ORDER BY now_paid DESC";
$result_user_paid = $connect->query($user_paid_query);
$users_paid = $result_user_paid->fetch_assoc();
$users_unpaid_query = "SELECT * from users WHERE now_paid=0 ORDER BY id ASC";
$users = array_merge($users_paid, $users_unpaid);
You can use union in MySQL itself(instead of PHP) to merge 2 SQL results.
(SELECT * from users WHERE now_paid != 0 ORDER BY now_paid DESC)
UNION
(SELECT * from users WHERE now_paid = 0 ORDER BY id ASC)
I was trying to use a select statement to get all the rows from a certain MySQL table except for three which has in user_id of 5,6,7. Below is the code but its not working properly. Please can you help.
$sql = "SELECT * FROM login ORDER BY user_id ASC LIMIT 0, 20 WHERE user_id<>5,6,7";
You have to Use NOT IN function for multiple id's.
$sql = "SELECT * FROM login WHERE user_id NOT IN (5,6,7) ORDER BY user_id ASC LIMIT 0, 20"
Do it like this:
SELECT * FROM login WHERE NOT user_id = 5, ORDER BY user_id ASC LIMIT 0, 20
$sql = "SELECT * FROM login WHERE user_id<>5 ORDER BY user_id ASC LIMIT 0, 20 "
Look at Order by, Where Clause and Order of operations
As you've just changed your question the new answer is
$sql = "SELECT * FROM login WHERE user_id NOT IN (5,6,7) ORDER BY user_id ASC LIMIT 0, 20 "
Look at NOT IN
Try this:
$sql = "SELECT * FROM `login` WHERE `user_id` NOT IN (5,6,7) ORDER BY `user_id` ASC LIMIT 0, 20";
I would like to get number of all records and get last record :
$sql_count_sms = "SELECT count(*) as total,content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = $row_num_sms['total'];
$last_my_sms = $row_num_sms['content'];
I can get number of records but I can't get last content record .
It returns first record !
Where is my wrong ?
Below codes works fine, but I think count(*) is faster than mysql_num_rows .
$sql_count_sms = "SELECT content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = mysql_num_rows($result_count_sms);
$last_my_sms = $row_num_sms['content'];
Any solution?
The grain of the two results you want is not the same. Without using a sub-query you can't combine an aggregate and a single row into the same result.
Think of the grain as the base unit of the result. The use of GROUP BY and aggregate functions can influence that "grain"... one result row per row on table, or is it grouped by user_id etc... Think of an aggregate function as a form of grouping.
You could break it out into two separate statements:
SELECT count(*) as total FROM android_users_sms WHERE user_id = :id;
SELECT * FROM android_users_sms WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Also, specific to your question, you probably want a LIMIT 1 in combination with the ORDER BY to get just the last row.
Now, counter intuitively perhaps, this should also work:
SELECT count(*), content, id
FROM android_users_sms
WHERE user_id = :id
GROUP BY id, content
ORDER BY id
LIMIT 1;`
This is because we've changed the "grain" with the GROUP BY. This is the real nuance and I feel like this could probably be explained better than I am doing now.
You could also do this with a sub query like so:
SELECT aus.*,
(SELECT count(*) as total FROM android_users_sms WHERE user_id = :id) AS s1
FROM android_users_sms AS aus
WHERE user_id = :id ORDER BY id DESC LIMIT 1;
My table structure is
I want to get the recent video uploaded of the user
What I want to do is:
"select video_id of a record who is having minimum of timestamp where userid='something'"
What I currently have is:
$recent_video = "$db->query("
SELECT video_id
FROM video_primary
ORDER BY timestamp DESC LIMIT 1
WHERE userid = '$userid'"
) or die($db->error);"`
while($row=mysqli_fetch_assoc($recent_video))
{
$video_id=$row['video_id'];
}
echo $video_id;
My table data is
How do I get the most recent view uploaded by the user?
Write WHERE clause after FROM part
Try this:
SELECT vp.video_id
FROM video_primary vp
WHERE userid='$userid'
ORDER BY vp.timestamp DESC
LIMIT 1;
try this
$recent_video=$db->query("select video_id from video_primary where userid='$userid'
ORDER BY timestamp DESC LIMIT 1 ") or die($db->error);
instead of
$recent_video="$db->query("select video_id from video_primary ORDER BY timestamp DESC
LIMIT 1 where userid='$userid'") or die($db->error);"
you are using where clause after order by and limit 1 which is a syntax error. see tutorial
Tutorial
There is a specific sequence of every clause in Query statement. Below is the sequence:
Select
Where
Order by
Limit
So, as per the above sequence, you should correct your query like below. Also, you should place double quote(") correctly.
$recent_video = $db->query("
SELECT video_id
FROM video_primary
WHERE userid = '$userid'
ORDER BY timestamp DESC LIMIT 1"
) or die($db->error);
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'