Joinen of tabellen geeft onjuiste format
Ik heb 2 tabellen
swap_group,
user_gold_sent
swap_group heeft deze velden
swap_id, user_id
user_gold_sent heeft deze velden
swap_id,
user_id,
gold_sent,
date
nu probeer ik dus het volgende het laten joinen van de tabellen met een left join.
alleen de uitkomst klopt niet met wat ik graag zou willen zien.
query die ik momenteel uitvoer
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
SELECT
swap_group.swap_id,
swap_group.user_id,
user_gold_sent.swap_id,
user_gold_sent.user_id,
SUM(user_gold_sent.gold_sent) as total_gold
FROM swap_group
LEFT JOIN
user_gold_sent
on user_gold_sent.swap_id = swap_group.swap_id
swap_group.swap_id,
swap_group.user_id,
user_gold_sent.swap_id,
user_gold_sent.user_id,
SUM(user_gold_sent.gold_sent) as total_gold
FROM swap_group
LEFT JOIN
user_gold_sent
on user_gold_sent.swap_id = swap_group.swap_id
geeft result
Dit terwijl het resultaat zo moet komen
swap_id user_id swap_id user_id total_gold
1 0001 1 0001 200000000
1 0002 1 0002 10000
2 0001 2 NULL NULL
Iets zegt me dat ik de join verkeerd uitvoer maar ik zal niet weten hoe.
Ben er al 3 dagen mee aan het stoeien.
Gewijzigd op 22/03/2018 12:02:11 door Thomas de vries
Sum is een aggregate functie. Als je deze gebruikt zonder GROUP BY dan zul je altijd maar één rij terug krijgen. Dit is dan het totaal van alle gevonden records. Wat wil je bereiken? Het totaal per gebruiker?
Wat ik wil bereiken is het volgende.
pak alle records van swap_group.
Kijk dan of swap_id en user_id in de user_gold_sent tabel voorkomt.
Zo niet laat dan null zien
zo wel laat dan alle goud zien dat verstuurt met het user_id dat gelijk is aan swap_id
Eigenlijk bovenstaande
zodat ik een query kan uitvoeren zoals ongeveer dit
INSERT INTO old_user_gold_sent (swap_id,user_id,gold_sent,date ) values (1,001,1000,date)
dan voor de volgende rij ook maar dan met user id 002 etc. Maar ook alleen maar als user_id voorkomt in de eerst genoemde query.
Niels? :s
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT
sg.swap_id,
sg.user_id,
ugs.swap_id,
ugs.user_id,
SUM(ugs.gold_sent) as total_gold
FROM
swap_group sg
LEFT JOIN
user_gold_sent ugs
ON
ugs.swap_id = sg.swap_id
GROUP BY
sg.user_id
sg.swap_id,
sg.user_id,
ugs.swap_id,
ugs.user_id,
SUM(ugs.gold_sent) as total_gold
FROM
swap_group sg
LEFT JOIN
user_gold_sent ugs
ON
ugs.swap_id = sg.swap_id
GROUP BY
sg.user_id
of
heb nu deze code
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
include('inc/config.php');
$a = $db->query('SELECT * FROM swap_settings');
while($b = $a->fetch_assoc()) {
$query = $db->query('
SELECT
swap_group.swap_id,
swap_group.user_id,
user_gold_sent.gold_sent,
user_gold_sent.date,
swap_settings.id,
swap_settings.swap_value
FROM
swap_group
LEFT JOIN
user_gold_sent on swap_group.user_id=user_gold_sent.user_id
LEFT JOIN
swap_settings on swap_group.swap_id=swap_settings.id
WHERE
swap_group.swap_id = "'.$b['id'].'"');
while($res = $query->fetch_assoc()) {
if($res['gold_sent'] == NULL) {
echo 'INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("'.$res['swap_id'].'","'.$res['user_id'].'")<br>';
} else {
echo 'INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("'.$res['swap_id'].'","'.$res['user_id'].'")||';
echo 'INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("'.$res['swap_id'].'","'.$res['user_id'].'","'.$res['gold_sent'].'","'.$res['date'].'")<br>';
}
}
}
?>
include('inc/config.php');
$a = $db->query('SELECT * FROM swap_settings');
while($b = $a->fetch_assoc()) {
$query = $db->query('
SELECT
swap_group.swap_id,
swap_group.user_id,
user_gold_sent.gold_sent,
user_gold_sent.date,
swap_settings.id,
swap_settings.swap_value
FROM
swap_group
LEFT JOIN
user_gold_sent on swap_group.user_id=user_gold_sent.user_id
LEFT JOIN
swap_settings on swap_group.swap_id=swap_settings.id
WHERE
swap_group.swap_id = "'.$b['id'].'"');
while($res = $query->fetch_assoc()) {
if($res['gold_sent'] == NULL) {
echo 'INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("'.$res['swap_id'].'","'.$res['user_id'].'")<br>';
} else {
echo 'INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("'.$res['swap_id'].'","'.$res['user_id'].'")||';
echo 'INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("'.$res['swap_id'].'","'.$res['user_id'].'","'.$res['gold_sent'].'","'.$res['date'].'")<br>';
}
}
}
?>
Output is nu dit
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("1","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","103918751825235915003")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("1","103918751825235915003","10000","2018-03-21 12:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("1","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","112482028735297197048")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("2","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("2","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("2","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("2","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("3","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("3","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("3","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("3","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("4","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("4","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("4","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("4","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("5","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("5","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("5","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("5","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("6","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("6","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("6","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("6","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("7","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("7","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("7","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("7","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","103918751825235915003")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("1","103918751825235915003","10000","2018-03-21 12:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("1","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("1","112482028735297197048")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("2","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("2","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("2","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("2","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("3","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("3","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("3","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("3","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("4","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("4","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("4","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("4","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("5","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("5","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("5","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("5","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("6","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("6","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("6","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("6","100886610003250557837","1000000000","0000-00-00 00:00:00")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("7","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("7","100886610003250557837","2961562094","2018-03-21 07:48:47")
INSERT INTO unfinished_swap_group (swap_id,user_id) VALUES ("7","100886610003250557837")||INSERT INTO user_gold_sent_unfinished_swap (swap_id,user_id,gold_sent,date) VALUES ("7","100886610003250557837","1000000000","0000-00-00 00:00:00")
Op zich bijna goed. Maar hij herhaalt de 2de while loop nog een paar keer ivm de eerste while loop.
Dit moet dan dus niet zo zijn.
Dan zou ik op zijn minst database-transacties verwachten, om de ondeelbaarheid van dit soort acties (beter) te garanderen.