Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a databade table as follows
id A
1 2
2 4
3 5
4 9
I want the result to show as below by doing repeated addition
A B
2 2
4 6
5 11
9 20
At this point, we're supposed to ask you what you've tried, but anyway...
SELECT x.a
, SUM(y.a) b
FROM my_table x
JOIN my_table y
ON y.id <= x.id
GROUP
BY x.id;
http://sqlfiddle.com/#!9/d66f6/1
SELECT a,
IF(#b IS NULL, #b:=a, #b:=#b+a) as B
FROM table1
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the follow table structure:
COLUMN1 COLUMN2 COLUMN3
1 6 1
3 3 7
4 8 9
I'd like to get the average of all columns. It's possible to get this with a single SQL command, with PHP prepared statements?
Thanks
I think you want to take the averages of each columns seperately. Above answer works but another answer can be as like below;
SELECT
SUM (COLUMN1)/ COUNT(COLUMN1) AS AVG1,
SUM (COLUMN2)/ COUNT(COLUMN2) AS AVG2,
SUM (COLUMN3)/ COUNT(COLUMN3) AS AVG3
FROM TABLE
SELECT
AVG (COLUMN1) AS AVG1,
AVG (COLUMN2) AS AVG3,
AVG (COLUMN3) AS AVG3
FROM TABLE
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Just as the title says, I need a query to count the rows until the condition is met. Here's my setup:
partnumber
-----------
b
e
d
a
c
So if I'm going to search for partnumber = d it will sort the part number and return 4 (since d is the 4th when you sort the partnumber).
I can do this inside a loop. I'm just wondering if there is a query for this.
Thanks in advance
SELECT COUNT(*) FROM THE_TABLE WHERE partnumber <= (SELECT ID FROM THE_TABLE WHERE partnumber <= 'd');
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a table named 'prices' and every range has its price.
course_id start end price
-----------------------------
1 1 5 20
1 5 10 18
1 10 100 15
-------------------------
I have for example $course_id = 1 and $weeks_num = 8.
How can I get the price of that course?
In the given example I should have 18 because 8 exists between 5 and 10.
You can use BETWEEN clause. http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between
...
WHERE 8 BETWEEN start AND end
...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm thinking adding a number to a row, and in an array (or something) i can declare what it is.
Like this:
mySQL table
id type duration
1 2 5
2 3 4
3 6 10
PHP array or something
type 2 = jumping
type 3 = biking
type 6 = painting
This way it will be easier to add, or remove a "type".
But sorry, i have no idea how to do this.
The effect I'm looking for is to display
Johan was jumping for 5 minuts
Johan was biking for 4 minuts
Johan was painting for 10 minuts
Help much appreciated!
I would suggest to safe everything to a DB instead of hard-coding it in your code. So taking your table as basis:
Table: t1
id type duration
1 2 5
2 3 4
3 6 10
Table: t2
type_id whatever
2 jumping
3 biking
6 painting
You could use a SQL-Join to combine those two tables:
SELECT t1.id, t2.whatever, t1.duration
FROM t1
JOIN t2
ON t1.type = t2.type_id
will produce
1 jumping 5
2 biking 4
....
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My sample database query result looks like this.
ID Product Owner
1 iPhone Shanu
2 MotoX Shajan
3 HTC Amal
4 iPhone Jibi
5 MotoX Anil
6 iPhone Gregary
I like to get the number of each phones
I want the result like this:
iPhone 3
MotoX 2
HTC 1
This is a fairly basic query -
SELECT `product`, count(`product`)
FROM `table`
GROUP BY `product`;