我很惊讶地看到IS NULL和= NULL在select查询中产生不同的结果.他们之间有什么区别?什么时候用.如果你能详细解释我,我会很高兴的.
我刚刚意识到当任何参数为null时,JPQL where子句不能像我预期的那样工作.例如,这个简单的JPQL查询
SELECT en FROM Entity en WHERE en.name = :name
Run Code Online (Sandbox Code Playgroud)
当name参数为null时,即使数据库中存在名称设置为null的实体,也不会返回任何结果.
在那种情况下,Hibernate用SQL执行WHERE entity.NAME = null.显然,这不是由数据库处理的,因为标准定义IS NULL了空比较(参见SQL为null和= null).JPQL也有IS NULL运算符(http://docs.oracle.com/javaee/6/tutorial/doc/bnbuf.html#bnbvi),其行为与SQL完全相同(http://docs.oracle.com/javaee/6 /tutorial/doc/bnbuf.html#bnbvr).
我认为这是一个常见的情况,但快速搜索没有给我任何有趣的结果.
那么,有没有办法在查询中包含空值?到目前为止,我想出了以下内容:
SELECT en FROM Entity en WHERE (:name IS NULL AND en.name IS NULL) OR en.name = :name
Run Code Online (Sandbox Code Playgroud)
它工作正常,但看起来不优雅,尤其是在较大的查询中.
奖金问题:为什么JPQL模仿SQL这个奇怪的方面?
我有如下代码来获取装运数据在哪里pdf_url不是NULL;
$shipment_data = DB::table('shipment')->where([
'shipment_date' => '2017-12-11', ['pdf_url', '<>', 'NULL']])->get();
Run Code Online (Sandbox Code Playgroud)
这没有问题,我得到了我需要的数据,但是当我尝试使用相同的代码来获取带有pdf_urlis的数据时NULL,它没有结果。
$shipment_data = DB::table('shipment')->where([
'shipment_date' => '2017-12-11', ['pdf_url', '=', 'NULL']])->get();
Run Code Online (Sandbox Code Playgroud)
我缺少什么?我很确定数据库记录在那里。我也尝试了其他格式,但仍然没有结果;
$shipment_data = DB::table('shipment')->where([
'shipment_date' => '2017-12-11', ['pdf_url', 'NULL']])->get();
Run Code Online (Sandbox Code Playgroud)
和
$shipment_data = DB::table('shipment')->where([
'shipment_date' => '2017-12-11', 'pdf_url' => 'NULL'])->get();
Run Code Online (Sandbox Code Playgroud)
编辑:我可以使用whereRaw,但我更喜欢使用where。下面的代码没有问题;
$shipment_data = DB::table('shipment')
->whereRaw('shipment_date = "2017-12-11" AND pdf_url is NULL')->get();
Run Code Online (Sandbox Code Playgroud) 我刚开始学习SQL; 我创建了一张桌子.学习插入命令和插入值分为2行.但是我在第3个中插入了空值.
现在我想删除第3行,其中有2列没有值.
我正在使用以下查询:
delete employee where city=null;
Run Code Online (Sandbox Code Playgroud)
它似乎没有工作!
为什么以下查询返回空集?
SELECT *
FROM null_test_tab
WHERE col1 = NULL
ORDER BY id
Run Code Online (Sandbox Code Playgroud)
结果:
Empty set
Run Code Online (Sandbox Code Playgroud) 有桌子 Departments
CREATE TABLE Departments
(
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
name nvarchar(100) NOT NULL,
parentDepId int
);
Run Code Online (Sandbox Code Playgroud)
我需要计算行数parentDepId = NULL,但我的查询每次都返回零.
SELECT COUNT(id) as DepartmentsCount
from Departments
WHERE parentDepId = NULL;
Run Code Online (Sandbox Code Playgroud)
它出什么问题了?
我是C#的新手,我正在编写一个桌面应用程序来读取MS-Access Invoice表,以便我可以对其进行进一步处理.但是我无法获取我的"reader.HasRows"语句来返回SELECT语句中的行,即使表中有行也是如此.每次运行应用程序时,我都会显示"找不到发票记录"MessageBox.
我已经在这个类似问题的论坛上研究和阅读了其他几篇文章,但似乎都没有解决我的特定问题.我还在互联网上搜索了一个没有结果的解决方案.有人能帮助我发现我做错了吗?
private void AddContactsAcconutNumberToInvoices()
{
//Use a variable to hold the SQL statement.
string inputString = "SELECT Invoices.[Job_Name], * FROM Invoices " +
"WHERE Invoices.[Account_Number] = Null";
try
{
//Create an OleDbCommand object and pass in the SQL statement and OleDbConnection object
OleDbCommand cmd = new OleDbCommand(inputString, conn);
//Send the CommandText to the connection, and then build an OleDbDataReader.
OleDbDataReader reader = cmd.ExecuteReader();
// Read through the database and test the integrity of the data
if (reader.HasRows)
{ …Run Code Online (Sandbox Code Playgroud)