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
Related
I have multiple tables, I can list their rows to my website.
What I want is where last column has same value, I want to add up rows values and list as one line.
What I have:
Hioss
AUR
Top
1
1
0
Shen
Hioss
AUR
Top
1
1
0
Shen
Kanani
AUR
Jungle
1
1
0
Reksai
I don't want to get data like this.
If last column has same values (Shen) I want to sum int values and show as one line at my website.
What I want to do:
Hioss
AUR
Top
2
2
0
Shen
Kanani
AUR
Jungle
1
1
0
Reksai
My mysql query:
mysql_query("SELECT * FROM table1 UNION SELECT * FROM table2");
How can I do it? What should I do?
Try this. i dont know the fieldnames. You must change it
SELECT fieldnam1,fieldnam2, sum(fieldnam3),fieldnam4
FROM (
SELECT * FROM table1
UNION
SELECT * FROM table2
) as result
GROUP by fieldnam4;
How can I select last (=max) value from column and count of all rows in single query?
ID ITEM_ID VALUE
1 1 100
2 1 101
3 2 201
4 3 333
5 2 222
6 1 111
I want to select last / max value for particular ITEM_ID and count of all rows with this ID.
For ITEM_ID = 1 thus:
VALUE COUNT
111 3
My query is like this:
SELECT (SELECT COUNT(*) FROM table) AS count, (SELECT value FROM table ORDER BY id DESC LIMIT 1) AS value FROM table WHERE item_id = 1 LIMIT 1
It works but looks ... weird. Is there any better (simpler / faster) solution? Thanks
You need to do a GROUP BY on column ITEM_ID while getting the MAX() and COUNT() like
select max(value) as `VALUE`,
count(*) as `COUNT`
from your_table
group by ITEM_ID;
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;
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;
i have a table like this
id name date group_id
1 n1 1 1
2 n2 1 1
3 n4 2 2
4 n5 2 2
i want ton write a query to return the group_id without duplicate ordered by date ASC
$query = " SELECT DISTINCT group_id FROM table ORDER BY date ASC";
this query will return 2 , 1 but this query is just going to consider date of the first row of each group_id to order the results
like if i have table like this
id name price date group_id
1 n1 2300 1 1
2 n2 3000 3 1
3 n4 4000 2 2
4 n5 2000 2 2
second row with with '1' as group_id has the biggest date so i should get 1,2 as result but query doesn't care about second row with the '1' group_id and still return 2,1
it only cares about the first row of each id for ordering the results
hopefully there is a easy way to solve this and i dont need to do something wird like putting everything in the 2d array and order that then deleting duplicates
Try this
select group_id,max(date) as SortDate
from table
group by group_id
order by SortDate
If I understand your problem, it sounds like you need to group by the date first.
SELECT group_id
FROM (
SELECT
group_id,
min(date) as min_date
FROM table
GROUP BY group_id
) as t
ORDER BY t.min_date;