在 SQL Server 中,我使用以下代码删除带有约束的表:
IF OBJECT_ID('EMPLOYEES') IS NOT NULL
BEGIN
ALTER TABLE EMPLOYEES DROP CONSTRAINT EMP_DEPT_FK
DROP TABLE EMPLOYEES;
END
Run Code Online (Sandbox Code Playgroud)
如何在 Mysql 中完成同样的事情?
静态游标不允许修改数据,因为它是只读的,并且当使用“Where current of”执行时,它会按预期返回错误。到目前为止,一切都很好。但我惊讶地发现静态游标允许使用这样的变量修改数据。
DECLARE @nome varchar(100), @salario int,@idemp int
DECLARE contact_cursor CURSOR STATIC FOR
SELECT empno,ename, sal FROM emp
OPEN contact_cursor;
FETCH NEXT from contact_cursor into @idemp,@nome, @salario
WHILE @@FETCH_STATUS=0
BEGIN
If @salario < 5000
Update Emp
Set Sal = Sal * 1.1
where empno=@idemp --No error and do the update
--Where current of contact_cursor; --gives error
print @nome+' '+cast(@salario as varchar(100));
Run Code Online (Sandbox Code Playgroud)
FETCH NEXT from contact_cursor into @idemp,@nome, @salario
END
CLOSE contact_cursor;
DEALLOCATE contact_cursor;
Run Code Online (Sandbox Code Playgroud)
问题是:在这次更新中使用“where current”和用光标提取的变量有什么区别? 我有这张表,我想检索下订单数量最多的客户。
我写了这个查询:
select * from customers where cust_id=(
select cust_id from orders
group by cust_id
having count(*)>=all(select count(cust_id)
from orders
group by cust_id))
Run Code Online (Sandbox Code Playgroud)
我知道也许有更好的方法来做到这一点,但我惊讶地发现 'ALL' 可以与 '> =' 一起使用
根据我的理解,'ALL' 检查当前行是大于还是小于子查询中的所有行,但我从没想过可以将它与 '=' 一起使用。
如果我将它与 '=' 或 '>' 一起使用,查询不会像我期望的那样返回任何行。
但是如果我将它们一起使用 '>=' 查询会给我正确的结果。
是不是很奇怪?
无论如何,最后我写了这个查询:
SELECT *
FROM Orders, customers
WHERE orders.cust_id=customers.cust_id
and orders.cust_id IN
(SELECT TOP (1) o.cust_id
FROM Orders AS O
GROUP BY O.cust_id
ORDER BY COUNT(*) DESC);
Run Code Online (Sandbox Code Playgroud)
你有更好或更优雅的解决方案吗?
在 'ALL' 子句中使用 '>=' 是不是很奇怪?
谢谢你。