Count values in MySQL with specific ID - mysql

Ok, this should be simple:
ID | version | downloads
========================
1 | 1.0 | 2
1 | 1.1 | 4
1 | 1.2 | 7
1 | 1.3 | 3
2 | 1.0 | 3
2 | 2.0 | 3
2 | 3.0 | 13
I like to get the downloads of a specific product (ID) no matter which version.
This doesn't work
SELECT COUNT(*) AS downloads FROM table WHERE ID = 1
should return 2 + 4 + 7 + 3 = 16

Your output says that you want to sum the downloads column.
so you have to use sum aggregate function..
SELECT Sum(downloads) AS downloads FROM table WHERE ID = 1
If you want sum(downloads) for each ID,Just change the query as follow
SELECT ID,Sum(downloads) AS downloads FROM table group by ID
If you need total record counts then only use Count
SELECT Count(*) AS count FROM table WHERE ID = 1

SELECT SUM(downloads) FROM table WHERE ID = 1;

Try
SELECT SUM(downloads) AS downloads
FROM table
WHERE id = 1
Sum adds the values
You can also use group by to return the sum for each id
SELECT SUM(downloads) AS downloads
FROM table
GROUP BY id

I would go for a query that could show you all SUMs from all IDs then if you need you filter one in specific.
SELECT id,
SUM(downloads) as TotalDownloads
FROM table
GROUP BY id;
If you need to filter a specific id just add where id = 1
The result for this would be:
ID | TotalDownloads
========================
1 | 16
2 | 19

Related

How to get the column value based on multiple rows?

I want to check rows where uid equal both 1 and 2 and if they do, return cid. In this example there will only ever be 2, but if you know a way to return the CID for more than 2, that would be great too.
How can I most easily get the value where cid = 5 when I know both uid values? (1,2).
cid | uid |
------------
5 | 1 |
5 | 2 |
6 | 1 |
6 | 3 |
7 | 1 |
7 | 4 |
For pseudo sql, I am thinking something like SELECT cid WHERE uid = 1 or uid = 2
This returns all rows where uid has a 1 or a 2. How can I limit to an OR statement and an AND?
SELECT cid WHERE uid = 1 AND uid = 2 (but in multiple rows)
Any ideas?
As far as i understand you're looking for a way to apply a condition to multiple rows, a way to do that is through agrupation functions. try this:
Select CID
from YourTable where uid IN (1,2)
group by cid
having count(uid) = 2
in this example i'm using IN instead of two OR and i'm grouping the rows by CID, and after that i'm limiting the results to those rows that match with UID equals to 1 and 2.
There are many tricky ways of achieve the same result, for example you can also do something like:
Select CID
from YourTable where uid IN (1,2)
group by cid
having sum(uid) = 3
in this example i'm suming the UID column, if UID is 1 and 2 the sum of both will result on 3, I assume that you can't have 3 rows with the UID 1 and the same CID
According to given details, Try this. Let's say you have a table called docs;
SELECT d1.cid
FROM docs AS d1
LEFT JOIN docs AS d2 ON d1.cid = d2.cid
WHERE d1.uid = 1
AND d2.uid = 2

Select previous record from row

Example
id |u_id
1 | 1
2 | 1
3 | 2
4 | 1
5 | 2
6 | 3
I know, the id 4 has u_id of 1, but I want to select the last row having u_id 1 before that with id 4 i.e. I want to select the row with the id 2.
Note that I don't know this id.
How can I achieve that?
This is what the result should look like:
id |u_id
2 | 1
4 | 1
select * from table where uid=1 order by id desc limit 2
This may help you.
SELECT * FROM ( SELECT * FROM yourTable WHERE u_id=1 ORDER BY id DESC LIMIT 2) AS tab ORDER BY tab.id ASC
Finally figured out the correct sql query for it.
SELECT * FROM table WHERE u_id = 1 AND (id = 4 OR id < 4) ORDER BY id DESC LIMIT 0,2
If you are using a fairly recent version of MySQL, what you need is the LAG() windowed function:
SELECT id,
u_id,
LAG(id) OVER W AS prev_id
FROM MyTable
WINDOW w AS (PARTITION BY u_id ORDER BY id)
ORDER BY id, u_id;
It will produce a result like this:
id |u_id |prev_id
1 | 1 | null
2 | 1 | 1
3 | 2 | null
4 | 1 | 2
5 | 2 | 3
6 | 3 | null
You can play with the query here.
As your title states:
select previous record from row
The following gives you every row that comes prior to a 2, and will work with more than just the data you've shown in your example:
SELECT *
FROM example
WHERE id IN (
SELECT id - 1
FROM example
WHERE u_id = 2);
[SEE DEMO HERE]

Find multiple totals by adding values from mysql table

I need to create a number adding all the values i can find in the db related to a specific customer.
Ex.
| Cust. | Value |
| 1 | 3 |
| 2 | 1 |
| 1 | 1 |
| 2 | 1 |
| 3 | 5 |
The result i want is : Customer #1 = 4, Customer #2 = 2; Customer #3 = 5.
There is a way to do that right into the mysql query?
Try Below query.
Select CONCAT('Customer #' , cust) as customer , sum(Value)
FROM customer_table
Group By cust
You want to SUM the values with a specific GROUP BY clause. Think of the GROUP BY as dividing rows into buckets and the SUM as aggregating the contents of those buckets into something useful.
Something like:
SELECT SUM(Value) FROM table GROUP BY Cust

my sql order by with values

I have a list of products which have an ID column as primary key, and category id which is a foreign key.. I want to overthem by a certain way.
ID | Name | CategoryID
----------------------------
1 | one | 1
2 | two | 2
3 | three | 1
4 | four | 3
5 | five | 5
6 | six | 4
7 | seven | 2
8 | eight | 1
if the above is my table. I want to get them in an SQL like the folowing
if want order these products in a certain way where all the products of category 5 needs to be
appearing first I am running a query like this.
SELECT * FROM Product ORDER BY CategoryID IN (5), ID
this does the job well.
but now i am in need to show the category id 5 first and then category id 2 first and the rest
if I try
SELECT * FROM Product ORDER BY CategoryID IN (5), CategoryID IN (2), ID ASC
that doesnt work.
any suggestions
You can use MySQL's FIELD() function:
SELECT * FROM Product ORDER BY FIELD(CategoryID, 2, 5) DESC
See it on sqlfiddle.
Try this way:
SELECT * FROM Product
ORDER BY
case CategoryID
when 5 then 0
when 2 then 1
else 2
end
, ID

Using SQL to get distinct rows, but also the whole row for those

Ok so its easier to give an example and hopefully some has a solution:
I have table that holds bids:
ID | companyID | userID | contractID | bidAmount | dateAdded
Below is an example set of rows that could be in the table:
ID | companyID | userID | contractID | bidAmount | dateAdded
--------------------------------------------------------------
10 | 2 | 1 | 94 | 1.50 | 1309933407
9 | 2 | 1 | 95 | 1.99 | 1309933397
8 | 2 | 1 | 96 | 1.99 | 1309933394
11 | 103 | 1210 | 96 | 1.98 | 1309947237
12 | 2 | 1 | 96 | 1.97 | 1309947252
Ok so what I would like to do is to be able to get all the info (like by using * in a normal select statement) the lowest bid for each unique contractID.
So I would need the following rows:
ID = 10 (for contractID = 94)
ID = 9 (for contractID - 95)
ID = 12 (for contractID = 96)
I want to ignore all the others. I thought about using DISTINCT, but i haven't been able to get it to return all the columns, only the column I'm using for distinct.
Does anyone have any suggestions?
Thanks,
Jeff
select *
from mytable main
where bidAmount = (
select min(bidAmount)
from mytable
where contractID = main.contractID)
Note that this will return multiple rows if there is more than one record sharing the same minimum bid.
Didn't test it but it should be possible with this query although it might not be really fast:
SELECT * FROM bids WHERE ID IN (
SELECT ID FROM bids GROUP BY contractID ORDER BY MIN(bidAmount) ASC
)
This would be the query for MySQL, maybe you need to adjust it for another db.
You could use a subquery to find the lowest rowid per contractid:
select *
from YourTable
where id in
(
select min(id)
from YourTable
group by
ContractID
)
The problem is that distinct does not return a specific row - it return distinct values, which ( by definition ) could occur on multiple rows.
Subqueries are your answer, and somewhere in the suggestions above is probably the answer. Your subquery need to return the ids or the rows with the minimum bidvalue. Then you can select * from the rows with those ids.