Getting Total Votes Form mysql table and Compare it? - php

I have a table with
id primary key, nominee_id, cat_id, user_id(voter id), vote_status, date
these fields, We have nominees for this voting process under different categories, so each nominees can be nimnated to more than one category. ok, all process is going well, I can take total count of votes got for each nominees in each category. but I dont have any idea How to get the winner from this table using SQL.
I want to get Most votes gained Nominees in each category, however as I said I can get total votes got for each nominees in each category using
SELECT nominee_id FROM voting WHEREcat_id= $cid.
Is it possible get this through another SQL statement, or else can anyone suggest any other way to get this.
below is the table, I want to get back the Nominee_id who got max Vote in a particulat cat_id, eg: in hte below table I want to get nominee_id 29 as a winner in cat_id 3, because he got two votes in that category

This query will give you the winner from each category.
SELECT nid, cid, max(votes) as final_votes from (select nominee_id as nid, cat_id as cid, count(user_id) as votes from voting group by cat_id, nominee_id) nv GROUP BY cid order by final_votes desc;

This one should work
SELECT nominee_id, COUNT(nominee_id) AS countNominee FROM voting WHERE
vote_status = 'yes' GROUP BY cat_id ORDER BY countNominee DESC

Related

How can I order query by another table?

I have a long query, but I keep it simplified:
$query = $this->db->query(" SELECT id FROM oc_products ORDER BY ...? ");
Here is the problem. In this table I have all the products, but I have a second table, oc_seller_products, where I have same column ID, which match with oc_products.
I want to be ordered first id's which dont appear in oc_seller_products, and at last, appear id's which appear in oc_seller_products.
For example: in oc_products I have ID: 1,2,3,4,5
In oc_seller_products I have only ID: 4
I need to be ordered like this: 5,3,2,1 and the last: 4
I have a marketplace, so I want first my products to appear, on category page, then my sellers products.
I really have no idea how to do that.
select op.id
from oc_products op
left join oc_seller_products osp using (id)
order by osp.id is null desc, op.id desc
osp.id is null will be 1 when there is not an oc_seller_products record and 0 when there is, so sort by that, descending, first. And then after that, within those two categories, you seem to want descending id order.

MySQL Get score of each user in each category

Basically i have the following tables:
users (id)
question_categories(id)
questions(id, category)
answers(id, user, question, score)
My problem is to get the total score of each user in each category in order to filter them based on the score in each category.
I expect a result table like (user, score_cat_1, score_cat_2, ...). I tried this option by building the table from PHP to add each category score column, but there is a join subquery I have to run on each category column I want to add. Something like
SELECT u.id, SUM(cat1.score) AS score_cat_1, ...
FROM users u
LEFT JOIN (
SELECT score, category, user
FROM answers a
JOIN questions q ON a.question=q.id AND q.category=1
) AS cat1 ON u.id=cat1.user
...
So I fear a performance issue with the size of that table and the number of categories. Is there a better approach?
Thanks

mysql searching row's based on AND

Hi guy's I've been trying to figure this out for hours, but I can't seem to wrap my head around it..
Take a look at this sql table..
I'm trying to search for a product_id that exists in all filter_cat_id's, so in this example it would be product_id 30, because it exists with filter_cat_id 1 2 and 3.. Product_id 30 exists in the same filter_cat_id twice, otherwise I'd just count the product id's, then the distinct cat id's and make sure the count's match.
Any ideas? Is this possible in mysql? another option would be to use PHP.
Thanks!
I'm trying to search for a product_id that exists in all filter_cat_id's, so in this example it would be product_id 30, because it exists with filter_cat_id 1 2 and 3.
In general, this can be done by grouping by product_id and using HAVING COUNT(product_id) >= x – where x is the number of categories,
SELECT product_id FROM table GROUP BY product_id HAVING COUNT(product_id) >= 3
If you don’t know the number of categories upfront (so that you can insert the actual number into the query), you can get that value with a subquery,
SELECT product_id FROM table GROUP BY product_id
HAVING COUNT(product_id) >= (SELECT COUNT(DISTINCT category_id) FROM table)
Edit: OK, so apparently the category_ids to search for come from some kind of search form/filter mechanism.
In that case, the filtering of items has to be added – and the number to compare to must be embedded implicitly, but that should be no problem.
SELECT product_id FROM table
WHERE category_id IN(1,2,3)
GROUP BY product_id
HAVING COUNT(product_id) >= 3
SELECT
product_id, count(i.product_id) as t
FROM
(SELECT
*
FROM
tablename
GROUP BY product_id , filter_cat_id) as i
GROUP BY product_id
HAVING t = 3
This should be smart enough to determine the number of distinct categories, and select a product_id that has that number of distinct categories.
SELECT product_id
FROM
(SELECT DISTINCT product_id, filter_cat_id
FROM test.play) a
GROUP BY product_id
HAVING count(product_id) = (SELECT count(DISTINCT filter_cat_id) FROM test.play)
;
Test Data:
Query Result:
30
40

Sorting count in multiple tables

I'm working on a PHP/mySQL table that shows data I've put into my database, and I'm trying to make it sortable. I have two tables in my database:
Table "restaurant" has columns: ID and name
Table "item" has columns: ID, name and restaurantID (restaurantID is set to use the IDs from the "restaurant" table)
What I want to do is sort the restaurants by the number of times their ID shows up in the item table. I'm sure there must be a simple way to do this, Just haven't been able to figure it out. Any help would be greatly appreciated!
Try this...
select r.name, count(i.ID)
from restaurant r
left join item i on i.restaurantID = r.ID
group by r.name
order by count(i.ID) desc
I believe you can do it by using a query like following;
SELECT restaurant.*, COUNT(items.id) AS item_id FROM restaurants, items WHERE restaurant.id = items.restaurant_id ORDER BY item_id ASC;
As you may know, sorting by multiple column is possible as well;
SELECT restaurant.*, COUNT(items.id) AS item_id FROM restaurants, items WHERE restaurant.id = items.restaurant_id ORDER BY item_id ASC, restaurant.`name` DESC;

Get Topics from multiple Category and show top ten topics with responses in mysql/php

Hi friends i am listing topics in php which may be associated with multiple category
my db schema is
topics
topic_id
user_id
topic_name
category
category_id
category_name
topic_category (for association)
tc_id
topic_id
category_id
topic_response // for results
tr_id
topic_id
response ( given in a form of 5 star rating so its in range of 1-5 always )
user_id
what i need to do is
1st ) list top ten topics based on responses ya it will be based on count of responses
i tried ->
select t.* ,count(tr.response) as votes from topics t , topic_response tr where t.topic_id=tr.topic_id group by tr.topic_id order by votes LIMIT 10
not working
2nd) user will be shown list of topic . he can choose the category in which he wants that can be multiple too.
for example
if he chooses category_id 1,2,3,4 then topics listed in this category will be listed.
i have tried to
select t.* from topics t ,topic_category tc where tc.topic_id = t.topic_id and category_id IN (1,3,2,4)
// not able to get idea on this i would prefer if i could do this in subquery since i also need to check if the user has already responded to that question .
**3) in case if i get a query working suppose .
from php side i will be getting an array of category_id from select multiple dropdown
like array(1,2,3,4)
so i was thinking how will make this query accept category id
in form of category_id IN (1,3,2,4) in mysql query**
**can i directly pass an array like
category_id IN ($ids) where $ids is an array**
i am a newbie in mysql please help me
you help will be appreciated :)
For the first question:
You have to use LEFT JOIN and match (response's topic id row) to (topic's id) then count(response's topic id) and GROUP everything by (response's topic id).
for example:
Responses Table = responses
response_id
topic_id
response_message
Topics Table = topics
id
title
content
The query is
SELECT topics.title,topics.content,COUNT(responses.topic_id) AS count FROM topics
LEFT JOIN responses ON topics.id = responses.topic_id GROUP BY count LIMIT 10
For question 2:
You can try using AND ( IN 1 OR 2 OR 3 OR 4
You can try using BETWEEN 1 AND 4
WHERE(category_id>=1&&category_id<=4)

Categories