where-clause

Why does a query slow down drastically if in the WHERE clause a constant is replaced by a parameter (having the same value)?

假装没事ソ 提交于 2019-11-30 14:51:46
问题 I have a recursive query which executes very fast if the WHERE clause contains a constant but becomes very slow if I replace the constant with a parameter having the same value. Query #1 - with constant ;WITH Hierarchy (Id, ParentId, Data, Depth) AS ( SELECT Id, ParentId, NULL AS Data, 0 AS Depth FROM Test UNION ALL SELECT h.Id, t.ParentId, COALESCE(h.Data, t.Data), Depth + 1 AS Depth FROM Hierarchy h INNER JOIN Test t ON t.Id = h.ParentId ) SELECT * FROM Hierarchy WHERE Id = 69 Query #2 -

Sequelize - subquery in where clause

点点圈 提交于 2019-11-30 12:52:01
问题 I'm using Sequelize in my Express app. I need to generate a query that has a subquery in the WHERE clause. SELECT * FROM MyTable WHERE id NOT IN ( SELECT fkey FROM MyOtherTable WHERE field1 = 1 AND field2 = 2 AND field3 = 3 ) I first tried relations/associations through my models but couldn't get it to work. Something like: MyTable.find( { where: { id: { $notIn: // <= what goes here? Can I somehow reference my include model? } }, include: [ { model: MyOtherTable, where: { field1: 1, field2: 2

Why does a query slow down drastically if in the WHERE clause a constant is replaced by a parameter (having the same value)?

感情迁移 提交于 2019-11-30 12:48:53
I have a recursive query which executes very fast if the WHERE clause contains a constant but becomes very slow if I replace the constant with a parameter having the same value. Query #1 - with constant ;WITH Hierarchy (Id, ParentId, Data, Depth) AS ( SELECT Id, ParentId, NULL AS Data, 0 AS Depth FROM Test UNION ALL SELECT h.Id, t.ParentId, COALESCE(h.Data, t.Data), Depth + 1 AS Depth FROM Hierarchy h INNER JOIN Test t ON t.Id = h.ParentId ) SELECT * FROM Hierarchy WHERE Id = 69 Query #2 - with parameter DECLARE @Id INT SELECT @Id = 69 ;WITH Hierarchy (Id, ParentId, Data, Depth) AS ( SELECT Id

Dynamic where clause in Linq to Entities

試著忘記壹切 提交于 2019-11-30 08:56:45
I'm using linq to entities(EF). I have a constructor which takes 4 string parameters. Depending on what parameter is not null I have to build the linq query. I can do with if else statements but i also has other constructor with 10 parameters in that case there will be many combinations to check. Example: Constructor(p1,p2,p3,p4) { var prod= from p in ctxt.products.expand("items\details") where p.x==p1 && p.xx==p2 && p.xxx==p3 && p.xxxx==p4 select p; } In the above where clause there should be condition checks only if the parameter is not null. ie., if p2 is null then the where clause should

using CASE in T-SQL in the where clause?

自闭症网瘾萝莉.ら 提交于 2019-11-30 08:06:31
问题 Im trying to use case to vary the value im checking in a where clause but I'm getting the error: incorrect syntax near the keyword 'CASE' SQL Server 2005 select * from table where ((CASE when adsl_order_id like '95037%' then select '000000'+substring(adsl_order_id,6,6) ELSE select adsl_order_id END) not in (select mwebID from tmp_csv_dawis_bruger0105) 回答1: Here is one way to include a case statement in a Where clause: SELECT * FROM sometable WHERE 1 = CASE WHEN somecondition THEN 1 WHEN

Linq To Sql 'Where Or' operator

蓝咒 提交于 2019-11-30 07:12:22
I need to create a query which checks if a field (string) contains one or more words supplied at run time. Basically I need to be able to ask a WhereOr question. This seems like it should be a common issue when dealing with LinqToSql. I found the following reference but can't make sense out of it - and have no idea how to use it in my project. i've tried the following loop: var query = from d in context.Domains select d; for (int i = 0; i < words.Length; i++) { query = query.Where(d => d.Name.Contains(words[i])); } but this builds a SQL query with WHERE AND Clauses NOT Where OR I use

PostgreSQL - count data as zero if it is null (when where clause is used)

倖福魔咒の 提交于 2019-11-30 05:28:49
问题 Now I have this query: SELECT opp.name as name, count(log.stage_id) as stage_count FROM crm_lead as opp LEFT OUTER JOIN crm_lead_stage_log as log ON (opp.id = log.opportunity_id) GROUP BY name And it outputs this result: name | stage_count | name1 | 2 name2 | 1 name3 | 0 And it outputs what I need. But if I put any condition to it, then it skips rows with zero count, which I need to be able to see. For example if I write this query: SELECT opp.name as name, count(log.stage_id) as stage_count

Dynamic Where Clause over relational tables with LINQ to SQL

人盡茶涼 提交于 2019-11-30 04:56:46
问题 I need help for a dynamic where clause over relational tables (one to many) in LinqToSql. User select conditions from page. (there is 4 input that user select the clauses) For example CompanyName and CompanyTitle from Customer table and OrderDate and ShipCity From Order table. But user can select one ore many of them from page interface and dynamic query will be generated at codebehind and select From LinqToSql. You can give similar type of example from another web pages. 回答1: Are you looking

SQL use column from subselect in where clause

给你一囗甜甜゛ 提交于 2019-11-30 03:15:24
I have a query that looks something like that: SELECT a, b, c, (SELECT d from B limit 0,1) as d FROM A WHERE d >= 10 I get the result that I want when I run the query without the where clause but when I add the where clause the query fails. Does anyone have a suggestion how to solve that? You can't use a column alias in WHERE clause. So you either wrap your query in an outer select and apply your condition there SELECT * FROM ( SELECT a, b, c, (SELECT d FROM B LIMIT 0,1) d FROM A ) q WHERE d >= 10 or you can introduce that condition in HAVING clause instead SELECT a, b, c, (SELECT d FROM B

CodeIgniter: How to use WHERE clause and OR clause

做~自己de王妃 提交于 2019-11-30 01:02:20
问题 I am using the following code to select from a MySQL database with a Code Igniter webapp: $query = $this->db->get_where('mytable',array('id'=>10)); This works great! But I want to write the following MySQL statement using the CI library? SELECT * FROM `mytable` WHERE `id`='10' OR `field`='value' Any ideas? Thanks! 回答1: $where = "name='Joe' AND status='boss' OR status='active'"; $this->db->where($where); 回答2: You can use or_where() for that - example from the CI docs: $this->db->where('name !=