I have a web form.I want to display the web form if the user hasn't submitted it once. If he had submitted it before the form will not appear. I have a table in sql like this below:
| userID | topicId | Date |
| 2 | 1 | 2018 |
| 2 | 5 | 2018 |
| 2 | 4 | 2018 |
| 2 | 8 | 2018 |
| 2 | 9 | 2018 |
My form is in PHP and I am struggling to write the script which can give me the count of topicId submitted by the userID. So :
if the count is less then 1 show him the topic web form;
if the count is bigger than 1 : don't show him the same topic web form.
So far I have this:
$query = $dbh->prepare("SELECT count(userID) from topicCountTable GROUP BY topicId");
$query->execute();
$row1 = $query->fetchAll();
if (count($row1) >1){
echo "Sorry you already submitted this topic";
}
else{
<form>
......
</form>
}
But for some reason, my script is not working
Did you check the query in SQL?
If you're using GROUP BY topic_id and you're looking for a specific topic_id value count, then the topic_id should be in the SELECT part:
SELECT topicId, count(userID) from topicCountTable GROUP BY topicId
You probably also want to specify the user_id you're looking for in the query in a WHERE, eg.
SELECT topicId, count(userID) from topicCountTable WHERE userID = 2 GROUP BY topicId
Related
I have a huge number of rows that I'd like to get say, last 5 records inserted in that database from 10 different users. If the same user inserted the last 3 rows into database, we must get one row, skip the others two and move to get a row per user, until it count up to 5.
A database like that:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
1 | 4 | baa
4 | 5 | baa0
5 | 6 | baa1
5 | 7 | baa2
6 | 8 | baa3
7 | 9 | baa4
Should return:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
4 | 5 | baa0
5 | 6 | baa1
The current filter was done by PHP, like this:
$used = array();
while ($data = mysql_fetch_array($query)) {
$uid = $data['user_id'];
if(in_array($uid, $used))
continue;
array_push($used, $uid);
// do something with data
}
But I want to refactor it, and do the filter purely by mysql, if possible. I don't know much MySql and that's why I'm having problem to archive this...
Here's what I've tried
select DISTINCT(user_id), news_id, title from XXX
WHERE GROUP BY (news_id) DESC
LIMIT 0,5
How can I do that?
1 way you can do it is to generate a partitioned row number per user and then select 5 records where RowNumber = 1.
SELECT *
FROM
(
SELECT
d.user_id
,d.news_id
,d.title
,(#rn:= if(#uid = user_id, #rn + 1,
if(#uid:=user_id,1,1)
)
) as RowNumber
FROM
Data d
CROSS JOIN (SELECT #uid:=-1, #rn:=0) vars
ORDER BY
user_id
,news_id
) t
WHERE
t.RowNumber = 1
ORDER BY news_id
LIMIT 5;
http://rextester.com/JRIZI7402 - example to show it working
Note you can change the row order by simply changing the ORDER BY statement of the derived table so if you have a column that will signify the latest record e.g. an identity column or a datetime column you can use that, but user_id must be the first criteria to be partitioned correctly.
Do it from your query.
"SELECT * FROM table GROUP BY user_id ORDER BY news_id DESC LIMIT 5"
well, i think this will achieve what you are after.
select user_id, news_id, title from tableName
GROUP BY user_id
ORDER BY news_id DESC
LIMIT 0,5
Hope this helps!
I have a really huge problem with my Social Network Plugin.
In my PHP script, I am concatenating SQL Where Queries, using conditional statements (if/else), because with the same function I'm getting the posts for the search bar and for the profile itselves.
I can't change, for this reason, the main statement.
I'm developing a Mention system, like Facebook's: When an user tags someone on its own profile, the post should be shown on the tagged user's profile too.
To achieve this, I'm editing the function that gets the posts, but can't figure out how to write the query.
Let's look the scenario:
I have two tables, one for posts, the other for the mentions linked to the posts' table.
Table: posts (user_id is the publisher, in_user_id is the profile where the post is published)
+---------+----------+------------+------------+------------+
| post_id | user_id | in_user_id | in_group | updated_at |
+---------+----------+------------+------------+------------+
| 1 | 12 | 12 | 0 | some_text |
| 2 | 1 | 1 | 0 | some_text2 |
| 3 | 2 | 2 | 0 | some_text3 |
| 4 | 5 | 12 | 0 | some_text4 |
| 5 | 5 | 5 | 0 | some_text4 |
| 6 | 5 | 5 | 0 | some_text4 |
+---------+----------+------------+------------+------------+
Table: posts_mentions (post_id is the post that contains the mention, user_id is the mentioned user)
+---------+----------+
| post_id | user_id |
+---------+----------+
| 2 | 12 |
| 3 | 12 |
| 6 | 1 |
+---------+----------+
What I need:
I need, when viewing the profile of user_id 12, to get the post_id of the posts published on 12's profile (posts.post_id 1 and 4), but also the posts where user_id 12 is tagged (posts.post_id 2 and 3), which are listed on posts_mentions.
What I tried:
The actual code is like this (this is an example):
$where="";
if($user_id->isFriend())
{
$where .= "WHERE (in_user_id = $user_id AND in_group = '0')";
}
...
$posts = $db->query(sprintf
("SELECT
*
FROM (SELECT posts.post_id
FROM posts ".$where.") posts
ORDER BY posts.post_id DESC");
(I used subqueries because otherwise I couldn't use conditional-concatenated queries)
I tried to add another sub-query in the $where statement, but it didn't work at all.
I'm really getting crazy. How can I resolve this problem?
You can add a subquery to get the post_ids from the post_mentions table. Something like:
$where .= "WHERE (in_user_id = $user_id AND in_group = '0')
OR post_id IN (
SELECT post_id FROM posts_mentions
WHERE user_id = $user_id
)
";
I am trying to built the messaging system and I want to select distinct value from the table and sort by date and also return the read and unread status. My table structure is as id, from_user_id, to_user_id, message, datetime, read . Suppose dummy data are as follows:
id | from_user_id | to_user_id | message | datetime | read
1 | 20 | 50 | hi | 2016-3-13 06:05:30 | 1
2 | 20 | 50 | hey | 2016-3-13 06:15:30 | 0
3 | 30 | 50 | hi | 2016-3-14 06:05:30 | 0
Now I want to select distinct from_user_id and sort by date so that I can display the name of the user who is chating recently on top of list and also return the read status 1, if any of the rows with from_user_id has read status 0, then I want to return read as 0 so that I can differentiate whose user message has unread message.
Suppose, from_user_id has read status as 0 in one rows then I want to return read status as 0 when returning distinct value of from_user_id. How can I do it. I tried as follows but won't work in my case. It returns distinct value but not sorted by date and also read status in not returning what I expect.
select distinct(`from_user_id`),register.name FROM message INNER JOIN register ON register.id = message.from_user_id where message.to_user_id = $user_id GROUP BY message.id ORDER BY MAX(datetime) ASC
Try the following query:
SELECT
message.from_user_id,
register.name,
message.read
FROM message
INNER JOIN
(
SELECT
from_user_id,
MAX(datetime) max_datetime
FROM message
WHERE to_user_id = 50
GROUP BY from_user_id ) t
ON t.from_user_id = message.from_user_id AND t.max_datetime = message.datetime
INNER JOIN register ON register.id = message.from_user_id
WHERE message.to_user_id = 50
ORDER BY message.datetime ASC
UPDATED SQL FIDDLE
Sample Input:
id | from_user_id | to_user_id | message | datetime | read
1 | 20 | 50 | hi | 2016-3-13 06:05:30 | 1
2 | 20 | 50 | hey | 2016-3-13 06:15:30 | 0
3 | 30 | 50 | hi | 2016-3-14 06:05:30 | 0
Output:
from_user_id name read
20 User20 0
30 User30 0
Note: This query might give you multiple row for the same from_user_id if there exists multiple entries in your message table having the same datetime and same to_user_id.
checkout this query :
SELECT DISTINCT(form_user_id),MIN(read) FROM table_name ORDER BY datetime ASC
Hello there, I have a schema like this, table name feeds
Where msg_id is unique and its the primary key of the table
| msg_id |commented|
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 21 |
I want to build a query that would select the last two rows
The output should go like this
| msg_id |commented|
| 3 | 10 |
| 4 | 21 |
In short the query should return the rows with msg_id which have a distinct commented value
Group by the column ment to be unique and select the highest id for every group
select max(msg_id) as msg_id, commented
from your_table
group by commented
Try this -
select max(msg_id), commented from your_table group by commented
SELECT * FROM feeds GROUP BY commented
I am developing an android Application that stimulates a poll notification feature. Therefore, I Created a service that keeps asking the server for the new data. However, I've used a table in my database called Seen. This table is used to be asked by the server for a specific user_id and if the news_id didn't exists in that table it will produce a notification.
The problem is when i launch the application for the first time. It retrieves all the data from the database because the server considering the users didn't see the news.
If Anyone Can Help me?
I Thought to solve it by this Idea: when I launch the application for the first time. Insert in seen table all of the news with that user_id in order to get 0 new messages. But i think it will be Not efficient.
This is my Database and my PHP script
Users table
User_ID | User_Name
--------------------
1 | John
2 | Carl
3 | Tomas
4 | Adam
5 | Nancy
News Table
News_ID | News_Text | news_date
---------------------------
1 | Hello World | CURRENTDATE()
2 | This is My car | CURRENTDATE()
3 | I had Ate pizza| CURRENTDATE()
4 | Leave Me Alone | CURRENTDATE()
5 | C++ Programming| CURRENTDATE()
Seen Table
ID | User_Id | News_Id
---------------------------
1 | 1 | 2
2 | 1 | 3
3 | 4 | 1
4 | 5 | 3
5 | 1 | 4
This is my PHP Code and it also showing my Query to get the news that didn't show in the Seen_news Table :
<?php
require('config.php');
$conn = mysqli_connect($servername, $username, $password, $db);
$query="SELECT * FROM news WHERE news_id NOT IN (SELECT news_id FROM news_seen WHERE user_id = '".$_GET['id']."')";
$result = mysqli_query($conn,$query);
$rows = array();
echo mysqli_error($conn);
while($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
echo json_encode($rows);
?>
Supposing that i am sending the User_Id to the PHP script and based on the result Query will show json file.
If you can add a create_date column to your Users table, you can select where Seen news_date is greater than User create_date.
Something like:
"SELECT * FROM news WHERE news_id NOT IN (SELECT news_id FROM news_seen WHERE user_id = '".$_GET['id']."') AND news_date > (SELECT create_date FROM user WHERE user_id = '".$_GET['id']."')"