MySQL select fields from row where criteria met, and field from row above
up vote
0
down vote
favorite
I have a data table of results where I would like to select information which is mostly held against the record relating to the second-place finisher.
So, for example, the location of the race, the date of the race, and the time deficit to the winner.
But what I would like to show in my query is the winner followed by their winning margin, which is actually stored against the record relating to the second-place finisher.
For example:
id | race_id | result | Entrant | Time
---|---------|--------|---------------|------
0 | 001 | 1 | Huw Edwards | 54:78.491
1 | 001 | 2 | Fiona Bruce | 0.250
2 | 001 | 3 | Sophie Raworth| 3.114
3 | 001 | 4 | George Alagiah| 6.756
4 | 001 | 5 | Mishal Husain | 42.452
5 | 001 | 6 | Peter Sissons | 58.004
6 | 002 | 1 | Fiona Bruce | 43:08.549
7 | 002 | 2 | Sophie Raworth| 5.439
8 | 002 | 3 | Mishal Husain | 5.444
9 | 002 | 4 | Huw Edwards | 6.007
10 | 002 | 5 | Peter Sissons | 13.000
11 | 002 | 6 | George Alagiah| 2:12.782
I have tried a query such as this (not working as desired):
SELECT r.id, r.entrant, r.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r.result = "2"
I'd like to end up with a result like the following, because it shows the winning margin that Huw Edwards enjoyed.
id | race_id | entrant | time
---|---------|-------------|-------
1 | 001 | Huw Edwards | 0.250
7 | 002 | Fiona Bruce | 5.439
Here is my table schema:
CREATE TABLE `races` (
`series` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`round` int(11) NOT NULL,
`date` date NOT NULL,
`race_id` text COLLATE utf8_unicode_ci NOT NULL,
`track` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`result` int(11) NOT NULL,
`number` int(11) NOT NULL,
`class` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`class_pos` int(11) NOT NULL,
`runner` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id` int(11) NOT NULL,
`runner2` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id2` int(11) NOT NULL,
`entrant` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`laps` int(11) NOT NULL,
`time` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`best` text COLLATE utf8_unicode_ci NOT NULL,
`qual` int(11) NOT NULL,
`marque` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43021 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
mysql database
add a comment |
up vote
0
down vote
favorite
I have a data table of results where I would like to select information which is mostly held against the record relating to the second-place finisher.
So, for example, the location of the race, the date of the race, and the time deficit to the winner.
But what I would like to show in my query is the winner followed by their winning margin, which is actually stored against the record relating to the second-place finisher.
For example:
id | race_id | result | Entrant | Time
---|---------|--------|---------------|------
0 | 001 | 1 | Huw Edwards | 54:78.491
1 | 001 | 2 | Fiona Bruce | 0.250
2 | 001 | 3 | Sophie Raworth| 3.114
3 | 001 | 4 | George Alagiah| 6.756
4 | 001 | 5 | Mishal Husain | 42.452
5 | 001 | 6 | Peter Sissons | 58.004
6 | 002 | 1 | Fiona Bruce | 43:08.549
7 | 002 | 2 | Sophie Raworth| 5.439
8 | 002 | 3 | Mishal Husain | 5.444
9 | 002 | 4 | Huw Edwards | 6.007
10 | 002 | 5 | Peter Sissons | 13.000
11 | 002 | 6 | George Alagiah| 2:12.782
I have tried a query such as this (not working as desired):
SELECT r.id, r.entrant, r.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r.result = "2"
I'd like to end up with a result like the following, because it shows the winning margin that Huw Edwards enjoyed.
id | race_id | entrant | time
---|---------|-------------|-------
1 | 001 | Huw Edwards | 0.250
7 | 002 | Fiona Bruce | 5.439
Here is my table schema:
CREATE TABLE `races` (
`series` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`round` int(11) NOT NULL,
`date` date NOT NULL,
`race_id` text COLLATE utf8_unicode_ci NOT NULL,
`track` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`result` int(11) NOT NULL,
`number` int(11) NOT NULL,
`class` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`class_pos` int(11) NOT NULL,
`runner` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id` int(11) NOT NULL,
`runner2` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id2` int(11) NOT NULL,
`entrant` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`laps` int(11) NOT NULL,
`time` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`best` text COLLATE utf8_unicode_ci NOT NULL,
`qual` int(11) NOT NULL,
`marque` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43021 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
mysql database
This isn't really enough information to help you. Where is the race ID stored? That's a necessary part of your query.
– Ed Cottrell♦
Nov 8 at 14:26
What's your logic in your question? it is not clear.
– D-Shih
Nov 8 at 14:28
The race ID is stored in the table 'races' if you hadn't worked it out...
– Andrew Abbott
Nov 8 at 14:42
How would your table structure support multiple races?
– Adder
Nov 8 at 15:01
There is a race ID field as well which isn't included in this example for simplicity.
– Andrew Abbott
Nov 8 at 15:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a data table of results where I would like to select information which is mostly held against the record relating to the second-place finisher.
So, for example, the location of the race, the date of the race, and the time deficit to the winner.
But what I would like to show in my query is the winner followed by their winning margin, which is actually stored against the record relating to the second-place finisher.
For example:
id | race_id | result | Entrant | Time
---|---------|--------|---------------|------
0 | 001 | 1 | Huw Edwards | 54:78.491
1 | 001 | 2 | Fiona Bruce | 0.250
2 | 001 | 3 | Sophie Raworth| 3.114
3 | 001 | 4 | George Alagiah| 6.756
4 | 001 | 5 | Mishal Husain | 42.452
5 | 001 | 6 | Peter Sissons | 58.004
6 | 002 | 1 | Fiona Bruce | 43:08.549
7 | 002 | 2 | Sophie Raworth| 5.439
8 | 002 | 3 | Mishal Husain | 5.444
9 | 002 | 4 | Huw Edwards | 6.007
10 | 002 | 5 | Peter Sissons | 13.000
11 | 002 | 6 | George Alagiah| 2:12.782
I have tried a query such as this (not working as desired):
SELECT r.id, r.entrant, r.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r.result = "2"
I'd like to end up with a result like the following, because it shows the winning margin that Huw Edwards enjoyed.
id | race_id | entrant | time
---|---------|-------------|-------
1 | 001 | Huw Edwards | 0.250
7 | 002 | Fiona Bruce | 5.439
Here is my table schema:
CREATE TABLE `races` (
`series` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`round` int(11) NOT NULL,
`date` date NOT NULL,
`race_id` text COLLATE utf8_unicode_ci NOT NULL,
`track` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`result` int(11) NOT NULL,
`number` int(11) NOT NULL,
`class` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`class_pos` int(11) NOT NULL,
`runner` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id` int(11) NOT NULL,
`runner2` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id2` int(11) NOT NULL,
`entrant` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`laps` int(11) NOT NULL,
`time` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`best` text COLLATE utf8_unicode_ci NOT NULL,
`qual` int(11) NOT NULL,
`marque` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43021 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
mysql database
I have a data table of results where I would like to select information which is mostly held against the record relating to the second-place finisher.
So, for example, the location of the race, the date of the race, and the time deficit to the winner.
But what I would like to show in my query is the winner followed by their winning margin, which is actually stored against the record relating to the second-place finisher.
For example:
id | race_id | result | Entrant | Time
---|---------|--------|---------------|------
0 | 001 | 1 | Huw Edwards | 54:78.491
1 | 001 | 2 | Fiona Bruce | 0.250
2 | 001 | 3 | Sophie Raworth| 3.114
3 | 001 | 4 | George Alagiah| 6.756
4 | 001 | 5 | Mishal Husain | 42.452
5 | 001 | 6 | Peter Sissons | 58.004
6 | 002 | 1 | Fiona Bruce | 43:08.549
7 | 002 | 2 | Sophie Raworth| 5.439
8 | 002 | 3 | Mishal Husain | 5.444
9 | 002 | 4 | Huw Edwards | 6.007
10 | 002 | 5 | Peter Sissons | 13.000
11 | 002 | 6 | George Alagiah| 2:12.782
I have tried a query such as this (not working as desired):
SELECT r.id, r.entrant, r.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r.result = "2"
I'd like to end up with a result like the following, because it shows the winning margin that Huw Edwards enjoyed.
id | race_id | entrant | time
---|---------|-------------|-------
1 | 001 | Huw Edwards | 0.250
7 | 002 | Fiona Bruce | 5.439
Here is my table schema:
CREATE TABLE `races` (
`series` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`round` int(11) NOT NULL,
`date` date NOT NULL,
`race_id` text COLLATE utf8_unicode_ci NOT NULL,
`track` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`result` int(11) NOT NULL,
`number` int(11) NOT NULL,
`class` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`class_pos` int(11) NOT NULL,
`runner` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id` int(11) NOT NULL,
`runner2` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`runner_id2` int(11) NOT NULL,
`entrant` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`laps` int(11) NOT NULL,
`time` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`best` text COLLATE utf8_unicode_ci NOT NULL,
`qual` int(11) NOT NULL,
`marque` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43021 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
mysql database
mysql database
edited Nov 9 at 16:09
Alex
14.1k11736
14.1k11736
asked Nov 8 at 14:24
Andrew Abbott
871219
871219
This isn't really enough information to help you. Where is the race ID stored? That's a necessary part of your query.
– Ed Cottrell♦
Nov 8 at 14:26
What's your logic in your question? it is not clear.
– D-Shih
Nov 8 at 14:28
The race ID is stored in the table 'races' if you hadn't worked it out...
– Andrew Abbott
Nov 8 at 14:42
How would your table structure support multiple races?
– Adder
Nov 8 at 15:01
There is a race ID field as well which isn't included in this example for simplicity.
– Andrew Abbott
Nov 8 at 15:08
add a comment |
This isn't really enough information to help you. Where is the race ID stored? That's a necessary part of your query.
– Ed Cottrell♦
Nov 8 at 14:26
What's your logic in your question? it is not clear.
– D-Shih
Nov 8 at 14:28
The race ID is stored in the table 'races' if you hadn't worked it out...
– Andrew Abbott
Nov 8 at 14:42
How would your table structure support multiple races?
– Adder
Nov 8 at 15:01
There is a race ID field as well which isn't included in this example for simplicity.
– Andrew Abbott
Nov 8 at 15:08
This isn't really enough information to help you. Where is the race ID stored? That's a necessary part of your query.
– Ed Cottrell♦
Nov 8 at 14:26
This isn't really enough information to help you. Where is the race ID stored? That's a necessary part of your query.
– Ed Cottrell♦
Nov 8 at 14:26
What's your logic in your question? it is not clear.
– D-Shih
Nov 8 at 14:28
What's your logic in your question? it is not clear.
– D-Shih
Nov 8 at 14:28
The race ID is stored in the table 'races' if you hadn't worked it out...
– Andrew Abbott
Nov 8 at 14:42
The race ID is stored in the table 'races' if you hadn't worked it out...
– Andrew Abbott
Nov 8 at 14:42
How would your table structure support multiple races?
– Adder
Nov 8 at 15:01
How would your table structure support multiple races?
– Adder
Nov 8 at 15:01
There is a race ID field as well which isn't included in this example for simplicity.
– Andrew Abbott
Nov 8 at 15:08
There is a race ID field as well which isn't included in this example for simplicity.
– Andrew Abbott
Nov 8 at 15:08
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
It is not 100% clear what do you want to get.
Please provide more examples with some other expected results.
So far, to give you some ideas of how you can get data you need:
http://sqlfiddle.com/#!9/7eb44f/2
SELECT r.id,
r.race_id,
r.entrant,
r1.entrant as Winner,
r1.time WinnerTime,
r.time
FROM races as r
LEFT JOIN races as r1
ON r1.result = 1
AND r1.race_id = r.race_id
WHERE r.result = 2
UPDATE Your table schema is terrible. to make my query faster for your table you should at least change race_id
column type integer (or varchar) but not text! And you need then to create an index over result
and race_id
columns:
ALTER TABLE races MODIFY race_id INTEGER;
CREATE INDEX result_race_id ON races (result, race_id);
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Provide your table schema:SHOW CREATE TABLE races;
?
– Alex
Nov 9 at 15:32
OMG!'time' varchar(256)
WHY?
– Alex
Nov 9 at 16:07
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
|
show 2 more comments
up vote
1
down vote
Your query looks pretty close, you just want to use the correct aliases - so you want r1.time and r1.result instead or r.time and r.result
SELECT r.id, r.entrant, r1.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r1.result = "2"
add a comment |
up vote
1
down vote
You can use a correlated query:
select id, entrant, (
select time
from races x
where race_id = races.race_id AND result = 2
) as win_margin
from races
where result = 1
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.x.raceid = races.raceid
)
– Salman A
Nov 8 at 22:36
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It is not 100% clear what do you want to get.
Please provide more examples with some other expected results.
So far, to give you some ideas of how you can get data you need:
http://sqlfiddle.com/#!9/7eb44f/2
SELECT r.id,
r.race_id,
r.entrant,
r1.entrant as Winner,
r1.time WinnerTime,
r.time
FROM races as r
LEFT JOIN races as r1
ON r1.result = 1
AND r1.race_id = r.race_id
WHERE r.result = 2
UPDATE Your table schema is terrible. to make my query faster for your table you should at least change race_id
column type integer (or varchar) but not text! And you need then to create an index over result
and race_id
columns:
ALTER TABLE races MODIFY race_id INTEGER;
CREATE INDEX result_race_id ON races (result, race_id);
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Provide your table schema:SHOW CREATE TABLE races;
?
– Alex
Nov 9 at 15:32
OMG!'time' varchar(256)
WHY?
– Alex
Nov 9 at 16:07
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
|
show 2 more comments
up vote
1
down vote
accepted
It is not 100% clear what do you want to get.
Please provide more examples with some other expected results.
So far, to give you some ideas of how you can get data you need:
http://sqlfiddle.com/#!9/7eb44f/2
SELECT r.id,
r.race_id,
r.entrant,
r1.entrant as Winner,
r1.time WinnerTime,
r.time
FROM races as r
LEFT JOIN races as r1
ON r1.result = 1
AND r1.race_id = r.race_id
WHERE r.result = 2
UPDATE Your table schema is terrible. to make my query faster for your table you should at least change race_id
column type integer (or varchar) but not text! And you need then to create an index over result
and race_id
columns:
ALTER TABLE races MODIFY race_id INTEGER;
CREATE INDEX result_race_id ON races (result, race_id);
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Provide your table schema:SHOW CREATE TABLE races;
?
– Alex
Nov 9 at 15:32
OMG!'time' varchar(256)
WHY?
– Alex
Nov 9 at 16:07
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
|
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It is not 100% clear what do you want to get.
Please provide more examples with some other expected results.
So far, to give you some ideas of how you can get data you need:
http://sqlfiddle.com/#!9/7eb44f/2
SELECT r.id,
r.race_id,
r.entrant,
r1.entrant as Winner,
r1.time WinnerTime,
r.time
FROM races as r
LEFT JOIN races as r1
ON r1.result = 1
AND r1.race_id = r.race_id
WHERE r.result = 2
UPDATE Your table schema is terrible. to make my query faster for your table you should at least change race_id
column type integer (or varchar) but not text! And you need then to create an index over result
and race_id
columns:
ALTER TABLE races MODIFY race_id INTEGER;
CREATE INDEX result_race_id ON races (result, race_id);
It is not 100% clear what do you want to get.
Please provide more examples with some other expected results.
So far, to give you some ideas of how you can get data you need:
http://sqlfiddle.com/#!9/7eb44f/2
SELECT r.id,
r.race_id,
r.entrant,
r1.entrant as Winner,
r1.time WinnerTime,
r.time
FROM races as r
LEFT JOIN races as r1
ON r1.result = 1
AND r1.race_id = r.race_id
WHERE r.result = 2
UPDATE Your table schema is terrible. to make my query faster for your table you should at least change race_id
column type integer (or varchar) but not text! And you need then to create an index over result
and race_id
columns:
ALTER TABLE races MODIFY race_id INTEGER;
CREATE INDEX result_race_id ON races (result, race_id);
edited Nov 9 at 16:13
answered Nov 8 at 14:40
Alex
14.1k11736
14.1k11736
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Provide your table schema:SHOW CREATE TABLE races;
?
– Alex
Nov 9 at 15:32
OMG!'time' varchar(256)
WHY?
– Alex
Nov 9 at 16:07
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
|
show 2 more comments
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Provide your table schema:SHOW CREATE TABLE races;
?
– Alex
Nov 9 at 15:32
OMG!'time' varchar(256)
WHY?
– Alex
Nov 9 at 16:07
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
I've edited my question now and realise I should have included the unique identifier for each race. Hopefully the addition of the second intended result row from the select statement helps. I've tried some of the responses in this thread, but it seems like they take longer than expected to process.
– Andrew Abbott
Nov 8 at 22:09
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Thanks for the edit. I've now run this on a data table of ~42,000 rows in phpmyadmin, but the query takes around 55 seconds. Is it expected to run slow on a dataset of 42k?
– Andrew Abbott
Nov 9 at 15:30
Provide your table schema:
SHOW CREATE TABLE races;
?– Alex
Nov 9 at 15:32
Provide your table schema:
SHOW CREATE TABLE races;
?– Alex
Nov 9 at 15:32
OMG!
'time' varchar(256)
WHY?– Alex
Nov 9 at 16:07
OMG!
'time' varchar(256)
WHY?– Alex
Nov 9 at 16:07
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
It sometimes contains comments.
– Andrew Abbott
Nov 9 at 16:24
|
show 2 more comments
up vote
1
down vote
Your query looks pretty close, you just want to use the correct aliases - so you want r1.time and r1.result instead or r.time and r.result
SELECT r.id, r.entrant, r1.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r1.result = "2"
add a comment |
up vote
1
down vote
Your query looks pretty close, you just want to use the correct aliases - so you want r1.time and r1.result instead or r.time and r.result
SELECT r.id, r.entrant, r1.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r1.result = "2"
add a comment |
up vote
1
down vote
up vote
1
down vote
Your query looks pretty close, you just want to use the correct aliases - so you want r1.time and r1.result instead or r.time and r.result
SELECT r.id, r.entrant, r1.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r1.result = "2"
Your query looks pretty close, you just want to use the correct aliases - so you want r1.time and r1.result instead or r.time and r.result
SELECT r.id, r.entrant, r1.time
FROM races as r
LEFT JOIN races as r1
ON r1.id > r.id
WHERE r1.result = "2"
answered Nov 8 at 14:35
Ian Kenney
5,35111636
5,35111636
add a comment |
add a comment |
up vote
1
down vote
You can use a correlated query:
select id, entrant, (
select time
from races x
where race_id = races.race_id AND result = 2
) as win_margin
from races
where result = 1
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.x.raceid = races.raceid
)
– Salman A
Nov 8 at 22:36
add a comment |
up vote
1
down vote
You can use a correlated query:
select id, entrant, (
select time
from races x
where race_id = races.race_id AND result = 2
) as win_margin
from races
where result = 1
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.x.raceid = races.raceid
)
– Salman A
Nov 8 at 22:36
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use a correlated query:
select id, entrant, (
select time
from races x
where race_id = races.race_id AND result = 2
) as win_margin
from races
where result = 1
You can use a correlated query:
select id, entrant, (
select time
from races x
where race_id = races.race_id AND result = 2
) as win_margin
from races
where result = 1
edited Nov 10 at 15:09
answered Nov 8 at 14:30
Salman A
173k66331419
173k66331419
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.x.raceid = races.raceid
)
– Salman A
Nov 8 at 22:36
add a comment |
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.x.raceid = races.raceid
)
– Salman A
Nov 8 at 22:36
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
What is the purpose of the x after races? Is it just an alias? It seems to work fine without it.
– Andrew Abbott
Nov 8 at 21:45
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.
x.raceid = races.raceid
)– Salman A
Nov 8 at 22:36
Yes it is an alias. It'll be useful if you want to reference the outer table (e.g.
x.raceid = races.raceid
)– Salman A
Nov 8 at 22:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209720%2fmysql-select-fields-from-row-where-criteria-met-and-field-from-row-above%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This isn't really enough information to help you. Where is the race ID stored? That's a necessary part of your query.
– Ed Cottrell♦
Nov 8 at 14:26
What's your logic in your question? it is not clear.
– D-Shih
Nov 8 at 14:28
The race ID is stored in the table 'races' if you hadn't worked it out...
– Andrew Abbott
Nov 8 at 14:42
How would your table structure support multiple races?
– Adder
Nov 8 at 15:01
There is a race ID field as well which isn't included in this example for simplicity.
– Andrew Abbott
Nov 8 at 15:08