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.
Related
I have a PHP system that allows users to vote photos on a scale of 1 - 5, what I want to do is highlight where two people give each other the same vote/score. I can't figure the SQL out at the moment for my PHP function.
The database looks like this
id, user_uid, voted_uid, score
As someone votes the id is auto incremental, the user_id is inserted from the session uid and the voted_uid comes from the image the user is viewing, then the score is the ranking from 1-5.
In theory we are therefore looking for two similar rows like this:
uid user_uid voted_uid score
7 3 5 3
38 5 3 3
At this point I want my php function to take the current users session and then match their votes and scores with other users.
In the example above I'd have the session id of 3 and I want it to return these two records as matches.
If I understand you correctly, what you want is to find pairs of rows where user_uid of the first row equals voted_uid in the second row and vice versa. But only, if score is the same in both rows.
In that case, this should do the trick:
SELECT a.*
FROM table AS a
JOIN table AS b
ON a.user_uid = b.voted_uid
AND a.voted_uid = b.user_uid
AND a.score = b.score;
If you only want rows that "mention" a specific uid, you of course have to add a WHERE user_uid = 3 OR voted_uid = 3.
I have an mysql database named gpstracking.
There are a couple tables in this database: positions and devices.
What is the issue.
On the table positions we have a row called device_id.
On the table devices we have a row called id.
On the table devices we have a row called name.
The device_id and id rows are the same id's, they are pointed to get the name.
My problem is, how to in PHP a loop or something to get the name when compare the 2 id rows.
So example:
device_id row:
1
2
3
4
5
id row:
1 Jan
2 Klaas
3 Piet
So if i fetch the information from table with the device_id row, i want that the script can get the name from the device_id.
Can someone help me ?
You mean this:
Select * from positions
join devices ON positions.device_id = devices.id
I would like to know if its possible to solve the following action with just one SQL-statement:
Table A: User-Table
Table B: Entries with foreign-keys for users
The relation between table A and Table B is 1 -> *
Because I don't want to count entries in table B for a specific user every time, I want to keep a column for this count in the user table. For that I would need a query that updates this column for each user.
Is this possible?
You can try this: UPDATE USERS U SET COUNT_ENTRIES=(SELECT COUNT(*) FROM ENTRIES WHERE USERID=U.ID)
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
Ok, I have 3 tables;
'bu_blogs' contains blogs which have a unique blog_id.
'bu_sites' contains sites which have a unique site_id.
'bu_blogs_done' contains id, blog_id and site_id. A new row is added to this table every time a site_id is submitted to a blog_id.
What I want to do is SELECT 2 random rows from 'bu_blogs' where a field in 'bu_blogs_done' for the particular blog_id and site_id does not exist, i.e it haven't been submitted to that blog_id yet.
Thanks
Stian
If your table isn't too big (e.g. approx 100 rows), you can use something like this simple example for the random part:
SELECT * FROM bu_blogs ORDER BY RAND() LIMIT 2
Then it's just a case of adding a WHERE clause to filter out the ones that exist in bu_blogs_done.