mysql | Group and count rows - php

I have a table and would like to calculate values. My table:
Table votes
vote type
1 1
-1 1
1 0
-1 0
1 0
-1 1
1 0
Vote: -1 - Down; +1 - Up;
I would like to get something like this:
Count votes up type 1: 1
Count votes down type 1: 2
Votes up type 0: 3
Votes down type 0: 1
Sum votes type 1: 1+(-1)+(-1) = -1
Sum votes type 0: 1+(-1)+1+1 = 2
Is it possible to get results from mysql using single query?
Thanks!

I guess your problem is how to distinct up and down votes here.
You want to group by type (which sounds like a vote/poll ID) and then just pick up and down votes. It can be done using CASE or IF in combination with COUNT or SUM.
COUNT does not count NULL values so I suggest to wrap two COUNTs in IFs.
SELECT
type,
COUNT(IF(vote = -1, TRUE, NULL)) AS down,
COUNT(IF(vote = 1, TRUE, NULL)) AS up
FROM votes
GROUP BY type
SQL Fiddle: http://sqlfiddle.com/#!2/36008/1/0

you can write
select count(*) from table where type = 1 and vote = -1
select count(*) from table where type = 1 and vote = 1
the above will give you the result count which you want .

This will work
select sum(vote),type from table group by type;

Related

Incrementing random cell in SQL with shared value

I want to select the lowest value in the 'n' sql column and increment it by 1. If the lowest value is present in multible rows, then I want to choose among those rows at random. For instance, in the example table below where the lowest number is 0 I want to randomly choose between the rows where ID = 1, 2, or 3.
ID
n
1
0
2
0
3
0
4
1
5
2
The code below will increment all three rows where n = 0. How do I randomly select just 1? I use Adminer as database.
$sql = "UPDATE studycondition SET n=n +1 WHERE n=(SELECT MIN(n) FROM studycondition)";
use
"UPDATE studycondition SET n=n +1 WHERE n=(SELECT MIN(n) FROM studycondition) limit 1"
Add limit in your inner query and update by id
UPDATE studycondition SET n = n + 1
WHERE id=(SELECT id FROM studycondition order by n asc limit 1)

Filtering SQL Results with Tags in a Junction Table [duplicate]

Lets consider the following table-
ID Score
1 95
2 100
3 88
4 100
5 73
I am a total SQL noob but how do I return the Scores featuring both IDs 2 and 4?
So it should return 100 since its featured in both ID 2 and 4
This is an example of a "sets-within-sets" query. I recommend aggregation with the having clause, because it is the most flexible approach.
select score
from t
group by score
having sum(id = 2) > 0 and -- has id = 2
sum(id = 4) > 0 -- has id = 4
What this is doing is aggregating by score. Then the first part of the having clause (sum(id = 2)) is counting up how many "2"s there are per score. The second is counting up how many "4"s. Only scores that have at a "2" and "4" are returned.
SELECT score
FROM t
WHERE id in (2, 4)
HAVING COUNT(*) = 2 /* replace this with the number of IDs */
This selects the rows with ID 2 and 4. The HAVING clause then ensures that we found both rows; if either is missing, the count will be less than 2.
This assumes that id is a unique column.
select Score
from tbl a
where a.ID = 2 -- based off Score with ID = 2
--include Score only if it exists with ID 6 also
and exists (
select 1
from tbl b
where b.Score = a.Score and b.ID = 6
)
-- optional? ignore Score that exists with other ids as well
and not exists (
select 1
from tbl c
where c.Score = a.Score and c.ID not in (2, 6)
)

count total numbers of same item exist in database

assume I have a table that contain a column named post_id and it has the result like
1
1
1
2
2
3
1
1
I want to loop through all the records and count how many times they exist. What I could thought of is
by while loop
if(result[] = 1){$1++}, but the problem is the value of record is not fixed, it can be 9999..
I'd tried
while ($something= $item->fetch_array()) {
while($test[] = $something['post_id'] > 0){
//logic here
}
}
select post_id, count(*)
from table
group by post_id
This is something you can do in SQL. I believe it would be the following:
SELECT post_id, COUNT(*) FROM tablename GROUP BY post_id;
This will return, for each post_id in the table, that post_id and the count of rows with that post_id.
Have you try this.
Assume:
Table one
**Table one**
Column1
1
1
1
2
2
3
1
1
You can use this query to count it.
SELECT one.column1, COUNT(two.column1) FROM one as one, one as two;

Find related posts and rank them by relevance

I have a website with different articles. The database structure is like this:
ArticleId | ArticleLocation | ArticleCategory | ArticleTopic
The actual text strings for the columns are in another table so all the columns are populated with numbers (integers)
I want to find related posts, meaning if a user reads an article with ArticleLocation = 1, ArticleCategory= 3 and ArticleTopic = 2, then I want to find top 5 of articles sharing the most of the same column values.
Any ideas of how to do this?
Thanks in advance
Assuming the table is named ArticleTable, try the following:
SELECT *, (
CASE ArticleLocation WHEN :loc THEN 1 ELSE 0 END +
CASE ArticleTopic WHEN :topic THEN 1 ELSE 0 END +
CASE ArticleCategory WHEN :cat THEN 1 ELSE 0 END) AS Relevance
FROM ArticleTable
ORDER BY Relevance DESC
LIMIT 5
with :loc, :topic and :cat set to the relevant values.

how to build a dynamic runtime query in mysql php?

I do not know how to classify this question. Vaguely, its about using calculated value in the WHERE clause of a mysql query performed using a php script.
Here's the scenario -
I've a mysql table with structure like this: table_id[int], item_id[int], item_rating[int]
Now the item_rating column can have either a "1" or a "0" value in it. The table_id column is set to auto_increment and the item_id column can have duplicate values also.
So a typical table will look like below -
table_id item_id item_rating
1 item1 1
2 item5 0
3 item1 1
4 item1 1
5 item5 1
6 item1 0
What i intend to do i for each item_id, i count the number of item_rating = 1 and item_rating = 0 and then take the difference of item_rating values to get the final rating for that item (final_item_rating = item_rating(with value=1) - item_rating(with value=0) ).
Now the issue:
I have a php script that takes values from these tables, and then displays the item details ordered on the "final_item_rating" value - something like:
select * from table_name order by final_item_rating desc
only problem is, since this final_item_rating is not a column in itself, and is actually based on run time value of the query, how do i build a query?
hope i have the question clear :)
This query may help you:
SELECT sum(item_rating) AS SumRatings
FROM table_name
WHERE item_rating=1
GROUP BY item_id
ORDER BY SumRatings;
This query would sum the ratings, and order the result with the highest rating on top:
select item_id
, sum(case when item_rating = 1 then 1 else -1 end) as rating
from YourTable
group by
item_id
order by
sum(case when item_rating = 1 then 1 else -1 end) desc

Categories