我正在尝试使用||编写linq-to-sql查询 当与LIKE/Contains结合使用时,其行为与SQL中的OR相同.
SQL:
SELECT * FROM Users
WHERE GroupNumber = 'A123456'
OR (FirstName LIKE 'Bob%' AND LastName LIKE 'Smith%')
Run Code Online (Sandbox Code Playgroud)
这将导致每个人的名字像"Bob Smith"以及GroupNumber等于A123456的每个人.在我的数据库中,sql示例给出了三个结果(所需的结果):
A123456 John Davis A312345 Bob Smith A123456 Matt Jones
Linq :(提供PNum = A123456; first ="Bob"; last ="Smith")
var users = from a in dc.Users
where a.PolicyNumber == PNum || (SqlMethods.Like(a.FirstName, first + "%") && SqlMethods.Like(a.LastName, last + "%"))
orderby a.PolicyNumber, a.FirstName
select a;
Run Code Online (Sandbox Code Playgroud)
这只会给我在||左侧的结果:
A123456 John Davis A123456 Matt Jones
我也试过a.Contains()和a.StartsWith(),但是每个版本我得到相同的两个结果.当我删除任何Like/Contain/StartsWith时,我得到了所需的结果,但我需要通配符.如何在Linq查询中获得所有三个结果?
我正在尝试使用JPA Criteria API实现以下类似的功能:
SELECT b FROM Box b JOIN SpecialItem s WHERE s.specialAttr = :specialAttr
Run Code Online (Sandbox Code Playgroud)
对象是
框
@Entity
public class Box implements Serializable {
...
@ManyToOne
@JoinColumn( name = "item_id" )
Item item;
...
}
Run Code Online (Sandbox Code Playgroud)
项目
@Entity
@Inheritance( strategy = InheritanceType.JOINED )
public class Item implements Serializable {
@Id
private String id;
...
}
Run Code Online (Sandbox Code Playgroud)
SpecialItem
@Entity
public class SpecialItem extends Item {
private String specialAttr;
...
}
Run Code Online (Sandbox Code Playgroud)
我的尝试
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery( …Run Code Online (Sandbox Code Playgroud) 我曾经使用python一段时间,但从来没有django.我正在接手一个不同的员工在离开公司之前做的项目.我想知道选项postgresql和postgresql_psycopg2作为django的数据库驱动程序之间是否存在差异.
在一些文章和关于如何设置django项目的文档中我只看到了postgresql,在其他文章中我看到了postgresql_psycopg2.我在提到psycopg2 的文档(这里 或这里)中找不到任何东西,所以这只是编写选项的旧方法吗?
一个只是另一个的别名还是它们实际上是不同的enignes?我也找不到任何其他SO问题.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',# here I also saw postgres_psycopg2
'NAME': 'premqcsite',
'USER': 'django_user',
'PASSWORD': 'Encepta_123',
'HOST': 'localhost',
'PORT': '5432',
}}
Run Code Online (Sandbox Code Playgroud) 我知道这可能是愚蠢的,但我决定以任何方式提问.
我一直试图查询类似的东西:
cursor.execute("select col1, col2 \
from my_tablem \
where afield like '%%s%'
and secondfield = %s
order by 1 desc " % (var1, var2) )
Run Code Online (Sandbox Code Playgroud)
但我在同一句话中得到错误.它不喜欢我需要获得包含第一个%s值的所有结果的额外%.
想法?
TIA!
我有一个简单的查询,但遇到了LIKE在VBA中使用的问题.我在VBA中的SQL字符串是:
stsql1 = "Select Top 25 data.* from data where data.Description Like ('*') "
Run Code Online (Sandbox Code Playgroud)
当我在我的VBA代码中运行此sql字符串时,我没有返回任何记录,但是如果我将相同的字符串复制/粘贴到MS Access中的SQL视图中的查询中,则查询将返回我期望的值.在VBA中使用"Like"语法有诀窍吗?
如果有帮助,我可以提供额外的代码和数据库的小版本.
我试图在表中查询有限的结果集,以便在javascript中填充自动完成字段.因此,我使用LIKE运算符输入了部分字符串.
例如,如果我有一个表,例如:
tblPlaces
id country
1 Balanca
2 Cameroon
3 Canada
4 Cape Verde
5 Denmark
Run Code Online (Sandbox Code Playgroud)
为了这个例子,让我们说我想要返回两行 - 是的,对于这个例子,我在那里组建了一个国家;)我想优先考虑在国家开头匹配部分字符串的任何实例.我开始使用的查询是:
SELECT id, country FROM tblPlaces WHERE country LIKE 'ca%' LIMIT 2
Run Code Online (Sandbox Code Playgroud)
这按预期返回"喀麦隆"和"加拿大".但是,如果没有两个名称在一个单词的开头匹配字符串(例如'de'),我希望它在单词的其他地方查找.所以我修改了查询成为
SELECT id, country FROM tblPlaces WHERE country LIKE '%ca%' LIMIT 2
Run Code Online (Sandbox Code Playgroud)
然后返回'佛得角'和'丹麦',但这样做打破了我原来的'ca'搜索,现在返回'Balanca'和'喀麦隆'.
所以,我的问题是,如何使用单个查询来优先处理单词开头的匹配(也许我需要使用REGEXP?)我还假设如果'country'列被索引,这些比赛将至少以随后的字母顺序返回(即加拿大之前的喀麦隆等).
我在两个领域寻找任何类似的比赛时遇到了一些麻烦.例如,我有一个包含值的表:
CAR MAKE CAR MODEL
Ford Mustang (Shelby)
Toyota Corolla
Seat Leon
etc etc.
Run Code Online (Sandbox Code Playgroud)
我想通过搜索以下任何组合来获得结果"Ford,Mustang(Shelby)":
福特野马
或任何其他组合.
这可能吗?我有一个很好的搜索,但很难找到搜索术语来描述我的意思.
我有一个带有3种方法的服务类,服务类也使用了一些@Autowired注释.在3种方法中,我想模拟两种方法,但使用实际方法进行第三种方法.
问题是:
在isReadyToDeliver方法中,如果订单中的所有产品都可用(ProductState.AVAILABLE)并且订单状态已准备好发送(OrderState.READY_TO_SEND),则方法必须返回true.我写了两部分,但我无法将它们组合在一起,
我写了,return orderState.andThen(productState)但得到这个错误:
andThen(Function<? super Boolean,? extends V>)类型中的方法Function<Order,Boolean>不适用于参数(Function<Order,Boolean>)
public class OrderFunctions {
public Function<Order, Boolean> isReadyToDeliver() {
Function<Order, Boolean> orderState = o -> o.getState() == OrderState.READY_TO_SEND;
Function<Order, Boolean> productState =
o -> o.getProducts()
.stream()
.map(Product -> Product.getState())
.allMatch(Product -> Product == ProductState.AVAILABLE);
return ????? ;
//return orderState.andThen(productState);
//error: The method andThen(Function<? super Boolean,? extends V>) in the type Function<Order,Boolean> is not applicable for the arguments (Function<Order,Boolean>) …Run Code Online (Sandbox Code Playgroud) java lambda functional-programming java-8 functional-interface
sql ×5
sql-like ×5
java ×3
django ×2
mysql ×2
python ×2
spring ×2
autowired ×1
contains ×1
criteria-api ×1
cursor ×1
java-8 ×1
jpa ×1
lambda ×1
linq ×1
linq-to-sql ×1
ms-access ×1
postgresql ×1
python-3.x ×1
search ×1
spring-ioc ×1
spy ×1
testing ×1
vba ×1