我在我的 sql server 2012 数据库上运行以下查询。
select Appointment.AppointmentDate, Appointment.AppointmentTime, Auctioneer.AuctioneerName, Auctioneer.AuctioneerSurname, Buyer.BuyerName, Buyer.BuyerSurname
from Appointment
inner join Auctioneer on Appointment.AuctioneerId=Auctioneer.AuctioneerId
inner join Buyer on Appointment.BuyerId=Buyer.BuyerId
where AppointmentDate between '2017-01-01' and '2017-12-31'
Run Code Online (Sandbox Code Playgroud)
结果如下:
+-----------------+-----------------+----------------+-------------------+-----------+----------------+
| AppointmentDate | AppointmentTime | AuctioneerName | AuctioneerSurname | BuyerName | BuyerSurname |
+-----------------+-----------------+----------------+-------------------+-----------+----------------+
| 2017-10-23 | 13:00:00 | Mary | Borg | David | Borg |
| 2017-10-24 | 15:30:00 | Mary | Borg | Joseph | Sammut |
| 2017-11-03 | 09:30:00 | Joseph | Smith …Run Code Online (Sandbox Code Playgroud) 如何检索具有不止一次出现的唯一值对的行...这是我正在尝试的示例..
SELECT
column_fk_id1, column_fk_id2,
COUNT(distinct column_fk_id1, column_fk_id2) AS unique_pair_repeats
FROM table
WHERE column_fk_id1 IS NOT NULL
GROUP BY column_fk_id1, column_fk_id2
HAVING unique_pair_repeats > 2
Run Code Online (Sandbox Code Playgroud) 最近我在代码中遇到了一个错误,相当于这个片段:
create table testing(num int, dt date, istrue boolean);
insert into testing values (1, '2018-01-01', True);
UPDATE testing SET num = 2, istrue = null AND dt = '2018-01-01';
Run Code Online (Sandbox Code Playgroud)
UPDATE 语句实际上应该是:
UPDATE testing SET num = 2, istrue = null WHERE dt = '2018-01-01';
Run Code Online (Sandbox Code Playgroud)
然而 postgresql 很高兴地接受了 UPDATE 为有效并评估了代码:
null AND dt = '2018-01-01'
Run Code Online (Sandbox Code Playgroud)
在第二个等号作为布尔值之后,因此基于错误的逻辑更新了数据。
我已经在其他 SQL 风格中尝试过这个,但这是不允许的,他们想要一个 WHERE。
我不是 postgres 的人,但它看起来确实是一个非常奇怪的语法怪癖,还是一个错误?
我有2张桌子。首先"product" (id)。第二- "product_has_option_value" (product_id, option_value_id)。
我怎样才能获得id没有一些的产品option_value_id(例如 10)?“product_has_option_value”具有多对多关系。
好的。我想我找到了解决方案:
select p.id
from product p
where p.id not in
(select distinct(product_id)
from product_has_option_value
where option_value_id=543)
Run Code Online (Sandbox Code Playgroud) 当我请求我的数据库服务器 (MySQL) 时,使用 "show grants for current_user"
我注意到我有权执行程序:
SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, EXECUTE
Run Code Online (Sandbox Code Playgroud)
但是当我执行包含这些指令的 sql 文件时:
DROP FUNCTION IF EXISTS
DROP FUNCTION IF EXISTS
DROP PROCEDURE IF EXISTS
CREATE FUNCTION
CREATE PROCEDURE
DECLARE
CALL
Run Code Online (Sandbox Code Playgroud)
我有这个错误:
ERROR 1044 (42000) at line 16: Access denied for user
Run Code Online (Sandbox Code Playgroud)
第 16 行包含: CREATE FUNCTION function_name( .. )
我应该有其他补助金吗?
sSQL = "
SELECT ordID, ordName, ordDate, ordShipping, ordStateTax,
ordHandling, ordTotal, ordDiscount, ordPrivateStatus
FROM orders
WHERE (ordStatus = 7)
"
Run Code Online (Sandbox Code Playgroud)
我想将昨天收到的订单显示为我正在构建的每日报告脚本
我该怎么办?
SQL Server 2005
ordDate 有这种格式: 2015-01-28 14:58:49.000
我需要UPDATE这样使用:
update offer set (bank, bank_id) = (cost, gid)
from (
select cost, osm_points.gid from pgr_kdijkstraCost(
'SELECT gid as id, source, target, walk_cost as cost FROM ways',
offer.vertice_id,
array(select vertice_id from osm_points where super_type = 'bank'),
false,false)
join osm_points on id2 = vertice_id
where cost != -1
and osm_points.super_type = 'bank'
order by cost
limit 1)t ;
Run Code Online (Sandbox Code Playgroud)
但我得到以下信息:
错误:对表“offer”的 FROM 子句条目的引用无效
提示:表“offer”有一个条目,但不能从查询的这部分引用它。
先感谢您
在我的代码中,我使用select * into #tempTable. 临时表包含大量记录,我需要在代码中多次从中检索数据,因此我在其上创建了聚集索引。但不知何故,我没有按照创建的索引以正确的顺序获取数据。
我知道我可以简单地通过放置order by子句来做到这一点,但我的问题是为什么即使我强制优化器使用特定索引,我也没有以正确的顺序获取数据?
下面是我的示例代码:
----------- Sample code ----------------------------
;with CTE1
as (
select col1
,col2
,-- - -- - - from table1
union
select col1
,col2
,-- - -- - - from table2
-- few more tables in union --
)
, CTE2
as (
select *
,RowNumber = RowNumber()
over (order by case
when @sortOrder = 0
then case
when @sortColumn = 'Date'
then [tdate]......some more dynamic order by conditions..........
end …Run Code Online (Sandbox Code Playgroud) 我有sy_version一个有 2 列主键的表(mod_id, sub_mod)::
CREATE TABLE `sy_version` (
`mod_id` VARCHAR(2) NOT NULL,
`sub_mod` VARCHAR(30) NOT NULL,
`version` VARCHAR(50) NULL DEFAULT NULL,
`remark` VARCHAR(50) NULL DEFAULT NULL,
`update_date` DATETIME NULL DEFAULT NULL,
`download_link` VARCHAR(50) NULL DEFAULT NULL,
`file` BLOB NULL,
PRIMARY KEY (`mod_id`, `sub_mod`)
)COLLATE='utf8_general_ci'
ENGINE=MyISAM;
Run Code Online (Sandbox Code Playgroud)
我的数据是
CS;sbm_sl.exe;2015.11.01;IBS Sales App;2015-11-10 11:34:13;\N;0x73002E00000000
Run Code Online (Sandbox Code Playgroud)
我想使用 更新我的数据insert into on duplicate key,首先它工作正常。但是每次我执行时,我都会得到我的file列变成,我想要的结果是:当我想要设置的版本大于当前(原始)版本时,NULL文件变成。NULL命令如下:
INSERT INTO sbm_sys.sy_version (mod_id,sub_mod,`version`,remark,update_date,file)
values ('CS','sbm_sl.exe','2015.11.07.1','IBS Sales App','2015-11-10 11:34:13', NULL)
ON DUPLICATE …Run Code Online (Sandbox Code Playgroud) WITH t1 AS (SELECT a.id,
AVG(standard_qty) std_avg,
AVG(poster_qty) pos_avg,
AVG(glossy_qty) gloss_avg
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP BY 1),
t2 AS (SELECT MAX(std_avg) max_std_avg,
MAX(pos_avg) max_pos_avg ,
MAX(gloss_avg) max_gloss_avg
FROM t1)
SELECT std_id , max_std_avg, pos_id , max_pos_avg, glos_id , max_gloss_avg
FROM(SELECT
(SELECT id std_id FROM t1,t2 WHERE std_avg = max_std_avg),
(SELECT id pos_id FROM t1,t2 WHERE pos_avg = max_pos_avg),
(SELECT id glos_id FROM t1,t2 WHERE gloss_avg =max_gloss_avg)
)foo ,t1,t2
Run Code Online (Sandbox Code Playgroud)
结果:
std_id | max_std_avg| pos_id …Run Code Online (Sandbox Code Playgroud) mysql ×4
sql-server ×3
postgresql ×2
subquery ×2
update ×2
count ×1
cte ×1
distinct ×1
permissions ×1
select ×1