How can I avoid users to select the same data from database in my PHP(MySQL)
I have a function that will randomly select 5 unique id to get data from database, however if multiple users clicked "Select" (the random 5), some of the data matched the other. It should not be selected because it was for the 1st user who got it.
I tried this, and it works fine if 1 user is selecting but if there's multiple users are selecting, they get random 5 data but some of data matched to the other user.
SELECT id, item_name, category_id
FROM items_tbl
WHERE status = 0
ORDER BY Rand()
Limit 5;
user 1
item_1
item_2
item_3 **
item_4 **
item_5
user 2
item_3 **
item_7
item_4 **
item_8
item_9
3 and 4 should not be selected for the next user. I have my used_flg to identify if it's used or not.
Related
Suppose there are 100 entries in a table and the need is to fetch data from id 3 to id 7 ( means data from 5 id(s), leaving the id 1 & 2).
Is it possible to do so using php mysql?
In case of searching in a range of numbers something like this can help.
select * from test where id between 3 and 7;
Which is similar to this
select * from test where id >= 3 and id <=7;
In case you want results from 3 to 7 but without id number 6 then something like this can also help.
select * from test where id in (3,4,5,7);
And again the above query can be re-writen to this
select * from test where id >= 3 and id <=7 and id<>6;
Check also this example on SQL fiddle where you could also check the time needed for the query result.
I have two tables in my DB
1.USER TABLE
2.POST table (id,title)
3.FAVOURITE TABLE (id,post_id,user_id)
I want to get post that favourite by a particular user along with total favourites of each post.
SELECT post.title,COUNT(favourite.post_id) as total_favourites FROM post
JOIN favourite ON post.id=favourite.post_id
WHERE favourite.user_id=119 GROUP BY favourite.post_id
I got the result but there is a problem with total_favourites its always give count as 1 even if there is more than one favourite for particular post.
for eg:
favourite table
id post_id,user_id
1 1 1
1 1 2
in the above post has id 1 have two favourites but in my query i will get count as 1
An user only can mark as favourite a post one time, so the total count always be 1.
Try grouping by user_id instead.
Let's say I have a table that stores user info with each row structured like:
(int) FileId |(int) userId | (int) DownloadHits | (varchar) UserName
Each user may have a lots of files uploaded by their names.
I want to show a list of user that have top download hits, look like :
userId UserName DownloadHits
1 Key 120
2 Bob 50
3 Zero 15
I tried SUM method but it only show the top one user.
Is there any solution for this query ?
select userid,username,
sum(downloadhits) as theDowns
from tblName
group by userid,username
order by theDowns desc
The query below selects the 'loves' on an item. (think of it as similar to facebooks 'like' system.
There are two tables in use in this select. A link table (containing itemid, userid, lovetime) and this is joined to a users table in order to retrieve the username/user profile url etc.
$lovequery = "select love.lovetime, love.userid as ID, love_users.display_name, love_users.user_url
from ".$wpdb->prefix."comment_loves love
left join ".$wpdb->prefix."users love_users on love_users.ID=love.userid
where commentid = $itemid
order by love.lovetime desc
limit 4
";
The results are limited to 4 because I simply do not need any more data. The total count is stored separately in the actual item table to reduce queries.
Once the rows are retrieved from this query I iterate through the array, cross referencing against the total 'lovecount' and build a text string formatted like so:
You, John Smith, Joe Bloggs and 4 others love this.
This works fine however it fails if the logged in user (YOU) does not have the most recent 'lovetime'
What I want to do is have the currently logged in use always at the top of the returned results even if his/her 'lovetime' is older than the 4 most recent ones so that the string always begins with 'You' if the logged in user has 'loved' this item.
The logged in user id is available in the script as $userid.
To clarify
if I have the following table (the timestamps are written as simple UK dates for legibility purposes):-
userid commentid lovetime
34 3 02/10/2011
24 3 03/10/2011
13 3 06/10/2011
65 3 14/10/2011
1* 3 10/09/2011
* with userid 1 being the logged in user id
I would only get user id's 34,24,13,65 returned in that order due to ordering by 'lovetime'
What I want is for the results to return ideally 1,34,24,65. if that proves too tricky then getting 5 total rows when the userid exists would be okay also.
I hope this is clear enough, it was rather difficult to articulate.
How would I go about modifying the query to ensure the results are as described.
Many thanks.
You can order result by condition like ORDER BY (ID = "auth_user_id") DESC
I am working on an application with PHP + MySql. In my application I have a table for users, a table for relationships (friends, following, subscribed) and a table for posts. The main actions for users are:
A user has friends
A user can make post entries
A user can see the friends entries
And finally a user can block entries viewing for specific friends
If user A is friends with user B, then user A can see all entries from user B. But user B can restrict access to only a few friends for example. Now the query is: how can I manage these permissions? I was thinking of a table that stores each user that is blocked for viewing an specific entry, but this would't be a good idea once a single user can have several friends. So, how can I solve this? Can someone show me how to start? Maybe the right terms for searching on Google or a link for something similar.
You are on the right track. You are going to want to use linked tables. You would start with a table users. Each user has an id. Then create a table users_friends. This table would consist of two ids, user_id and friend_id. The last table would be users_restricted which would also consist of two ids, user_id and restricted_id.
For example
users
user_id name
1 user1
2 user2
3 user3
users_friends
friend1_id friend2_id
1 2
2 3
This says user 1 and 2 are friends and users 2 and 3 are friends. (This assumes that if user 1 is friends with user 2 then user 2 is also friends with user 1)
users_restricted
user_id restricted_id
1 2
Now even though user 1 and user 2 are friends, user 2 is in the restricted list meaning don't allow user 2 to view user 1's entries.
You can see that tables are linked via ids and all the ids come from the users table. This can be expanded to relate to entries as well. Just refer to entries by their id.
To have users blocked for specific entries you would have the following tables.
entries
entry_id user_id ... other columns holding entry information
1 1
2 1
3 2
4 2
Now user 1 has made 2 entries (entry 1 and entry 2) and user 2 has made 2 entries (entry 3 and entry 4). Another table would hold the restrictions.
entries_restricted
entry_id restricted_user_id
1 2
This would say user 2 cannot view entry 1.
To get the entries visible to user 2 your statement would look something like this.
SELECT e.*, er.entry_id FROM entries e JOIN entries_restricted er ON e.entry_id=er.entry_id WHERE er.restricted_user_id != 2;
The statement selects all the entry information excluding entries restricted to user 2.
You can start using following tables.
The first table is users table (as Jason.Wolfsmith suggested)
users
u_user_id u_name
1 user1
2 user2
3 user3
The second table can be like this.
friends_permissions
f_user_id f_friend_id permission entries
1 2 1 entry1
2 3 0
1 3 1 entry3
This table will contain permission and name of entries that should be allow for view. 1 - restrict some entries; 0 - allow all.
In the column permission data type might be set as SET('1','0') and data type in entries NULL.
Thus, user1 don't allow to view entry1 to user2. (entry1 and entry3 are from entries table).