การเชื่อมระหว่าง Table 1 และ Table 2 ซึ่งมี Field ที่ไม่เหมือนกันจึงจำเป็นต้องกำหนดให้ ข้อมูลตรงกันโดยคำสั่ง mysql CONCAT
Table 1
SELECT
rulecurve.r_date,
rulecurve.upper,
rulecurve.lower
FROM
rulecurve
WHERE
right(rulecurve.r_date,2) = '01'
ORDER BY
right(rulecurve.r_date,2) ASC,left(rulecurve.r_date,2) ASC
show data
r_date | upper | lower |
01/01 | 161.94 | 148.87 |
02/01 | 161.87 | 148.82 |
03/01 | 161.81 | 148.78 |
04/01 | 161.75 | 148.73 |
05/01 | 161.69 | 148.69 |
06/01 | 161.62 | 148.65 |
07/01 | 161.56 | 148.6 |
08/01 | 161.5 | 148.56 |
09/01 | 161.43 | 148.52 |
10/01 | 161.37 | 148.47 |
11/01 | 161.31 | 148.43 |
12/01 | 161.25 | 148.38 |
13/01 | 161.18 | 148.34 |
14/01 | 161.12 | 148.3 |
15/01 | 161.06 | 148.25 |
16/01 | 160.99 | 148.21 |
17/01 | 160.93 | 148.16 |
18/01 | 160.87 | 148.12 |
19/01 | 160.81 | 148.08 |
20/01 | 160.74 | 148.03 |
21/01 | 160.68 | 147.99 |
22/01 | 160.62 | 147.94 |
23/01 | 160.55 | 147.9 |
24/01 | 160.49 | 147.86 |
25/01 | 160.43 | 147.81 |
26/01 | 160.37 | 147.77 |
27/01 | 160.3 | 147.73 |
28/01 | 160.24 | 147.68 |
29/01 | 160.18 | 147.64 |
30/01 | 160.11 | 147.59 |
31/01 | 160.05 | 147.55 |
Table 2
SELECT plantreal.r_date,
format(plantreal.fore, 2) as forebay,
format(plantreal.`infl24`,2) as inflow
FROM plantreal
WHERE month (plantreal.r_date) = '01' and
year (plantreal.r_date) = year (curdate()) and
plantreal.r_time = '24:00'
ORDER BY plantreal.r_date ASC
show data
r_date | forebay | inflow |
1/1/2012 | 158.96 | 8.59 |
2/1/2012 | 158.85 | 9.82 |
3/1/2012 | 158.73 | 7.3 |
4/1/2012 | 158.6 | 8.87 |
5/1/2012 | 158.45 | 11.17 |
6/1/2012 | 158.28 | 9.28 |
7/1/2012 | 158.12 | 8.16 |
8/1/2012 | 157.99 | 9.09 |
9/1/2012 | 157.88 | 9.14 |
10/1/2012 | 157.75 | 8.24 |
11/1/2012 | 157.62 | 8.31 |
12/1/2012 | 157.48 | 5.82 |
13/1/2012 | 157.34 | 6.25 |
14/1/2012 | 157.21 | 8.46 |
15/1/2012 | 157.09 | 6.94 |
16/1/2012 | 156.98 | 9.37 |
17/1/2012 | 156.84 | 4.13 |
18/1/2012 | 156.71 | 6.75 |
19/1/2012 | 156.59 | 9.19 |
20/1/2012 | 156.46 | 6.96 |
21/1/2012 | 156.33 | 6.77 |
22/1/2012 | 156.22 | 9.58 |
23/1/2012 | 156.1 | 7.3 |
24/1/2012 | 155.97 | 8.9 |
25/1/2012 | 155.81 | 1.86 |
26/1/2012 | 155.61 | 0 |
27/1/2012 | 155.42 | 0.07 |
28/1/2012 | 155.23 | 1.4 |
29/1/2012 | 155.06 | 7 |
30/1/2012 | 154.88 | 4.21 |
31/1/2012 | 154.7 | 6.8 |
การเชื่อมระหว่าง Table 1 และ Table 2 ซึ่งมี Field ที่ไม่เหมือนกันจึงจำเป็นต้องกำหนดให้ ข้อมูลตรงกันโดยคำสั่ง mysql CONCAT =
concat(right(plantreal.r_date,2),"/",mid(plantreal.r_date,6,2)
ผลลัพธ์ 2012-01-11 เป็น 11/01
จากนั้นทำการ Join Table โดยเอา Table 2 เป็น หลัก
LEFT JOIN rulecurve ON (concat(right(plantreal.r_date,2),"/",mid(plantreal.r_date,6,2)) = rulecurve.r_date)
ผลลัพธ์ 11/01 = 11/01
Table 3
SELECT
plantreal.r_date,
format(plantreal.fore, 2) AS forebay,
rulecurve.r_date as ru_date,
rulecurve.upper,
rulecurve.lower
FROM
plantreal
LEFT JOIN rulecurve ON (concat(right(plantreal.r_date,2),"/",mid(plantreal.r_date,6,2)) = rulecurve.r_date)
WHERE
month(plantreal.r_date) = '05' AND
year(plantreal.r_date) = year(curdate()) AND
plantreal.r_time = '24:00'
ORDER BY
plantreal.r_date
show data
r_date | forebay | ru_date | upper | lower |
1/5/2012 | 141.56 | 01/05 | 152.41 | 137.62 |
2/5/2012 | 141.46 | 02/05 | 152.32 | 137.58 |
3/5/2012 | 141.35 | 03/05 | 152.23 | 137.53 |
4/5/2012 | 141.22 | 04/05 | 152.14 | 137.49 |
5/5/2012 | 141.09 | 05/05 | 152.05 | 137.45 |
6/5/2012 | 141.04 | 06/05 | 151.96 | 137.41 |
7/5/2012 | 141.03 | 07/05 | 151.86 | 137.37 |
8/5/2012 | 140.99 | 08/05 | 151.77 | 137.33 |
9/5/2012 | 140.98 | 09/05 | 151.68 | 137.29 |
10/5/2012 | 140.96 | 10/05 | 151.59 | 137.25 |
11/5/2012 | 140.9 | 11/05 | 151.5 | 137.2 |
12/5/2012 | 140.82 | 12/05 | 151.41 | 137.16 |
13/5/2012 | 140.73 | 13/05 | 151.32 | 137.12 |
14/5/2012 | 140.66 | 14/05 | 151.23 | 137.08 |
15/5/2012 | 140.57 | 15/05 | 151.14 | 137.04 |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น