这是一个sql查询尝试在MySql数据库表中join
两个不同的表,用于为单个用户提取PR_Cod
和PR_Cod
的值:
SELECT
CA.theDate,
CA.theHour,
CA.TheUSerCode,
A.PR_Cod,
A.PR_Name
FROM
tblUserRegistered CA
JOIN tblUserAreaGlobal A ON A.TheUSerCode = CA.TheUSerCode
WHERE
theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY)
AND '2015-11-16'
AND (
HOUR (theHour) >= '15:00:00'
OR HOUR (theHour) <= '03:00:00'
)
AND A.PR_Cod IN ('058')
ORDER BY
CA.TheUSerCode,
CA.theDate,
CA.theHour;
+------------+----------+-------------+--------+---------+
| theDate | theHour | TheUSerCode | PR_Cod | PR_Name |
+------------+----------+-------------+--------+---------+
| 2015-11-16 | 21:30:37 | EU4012111 | 058 | XXX1 |
| 2015-11-16 | 22:21:29 | EU4012111 | 058 | XXX1 |
| 2015-11-16 | 15:15:00 | EU4046905 | 058 | XXX1 |
+------------+----------+-------------+--------+---------+
3 rows in set
现在我需要在第一个表Value
上的字段Text
和tblUserRegistered
上更新这三行。
我尝试过这个update join
查询:
UPDATE tblUserRegistered AS target
INNER JOIN (
SELECT
CA.theDate,
CA.theHour,
CA.TheUSerCode,
A.PR_Cod,
A.PR_Name
FROM
tblUserRegistered CA
JOIN tblUserAreaGlobal A ON A.TheUSerCode = CA.TheUSerCode
WHERE
theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY)
AND '2015-11-16'
AND (
HOUR (theHour) >= '15:00:00'
OR HOUR (theHour) <= '03:00:00'
)
AND A.PR_Cod IN ('058')
ORDER BY
CA.TheUSerCode,
CA.theDate,
CA.theHour) AS source ON source.TheUSerCode = target.TheUSerCode
SET target.Value = 1,
target.Text = 'Y';
但是在输出中我没有更新只有第一行查询提取三行但更多一行:
+------------+----------+-------------+-------+------+
| theDate | theHour | TheUSerCode | Value | Text |
+------------+----------+-------------+-------+------+
| 2015-11-16 | 22:21:29 | EU4012111 | 1 | Y |
| 2015-11-16 | 21:30:37 | EU4012111 | 1 | Y |
| 2015-11-16 | 15:15:00 | EU4046905 | 1 | Y |
| 2015-11-16 | 04:22:13 | EU4046905 | 1 | Y |
+------------+----------+-------------+-------+------+
4 rows in set
为什么查询会以小时04:22:13
更新行?
我尝试过:
UPDATE tblUserRegistered AS target
INNER JOIN (
SELECT
CA.theDate,
CA.theHour,
CA.TheUSerCode,
A.PR_Cod,
A.PR_Name
FROM
tblUserRegistered CA
JOIN tblUserAreaGlobal A ON A.TheUSerCode = CA.TheUSerCode
WHERE
theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY)
AND '2015-11-16'
AND (
HOUR (theHour) >= '15:00:00'
OR HOUR (theHour) <= '03:00:00'
)
AND A.PR_Cod IN ('058')
ORDER BY
CA.TheUSerCode,
CA.theDate,
CA.theHour) AS source ON source.TheUSerCode = target.TheUSerCode
SET target.Value = 1,
target.Text = 'Y'
WHERE
target.theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY)
AND '2015-11-16'
AND (
HOUR (target.theHour) >= '15:00:00'
OR HOUR (target.theHour) <= '03:00:00'
)
AND source.PR_Cod IN ('058');
但是在这种情况下我有错误:
[Err] 1292 - Truncated incorrect DOUBLE value: '15:00:00'
请帮帮我,非常感谢你。
答案 0 :(得分:1)
您自己很复杂,只需查询并使用UPDATE语法
即可public List<PlainBrgMetric> GetPlainBrgMetricProgram(long programLOBID)
{
var query = _context.metrics.Join(_context.universals,
m => m.metricID,
u => u.orderByAsc,
(metric, universal) => new PlainBrgMetric
{
//Populate the object properties
...
}).ToList();
return query;
}