where-clause

Why would a sql query have “where 1 = 1” [duplicate]

隐身守侯 提交于 2019-11-28 10:12:16
I was going through a few queries I am maintaining, and a programmer had put in the queries "where 1=1" to me that always seems to evaluate to true. Are there benefits to this? Duplicate: Why would someone use WHERE 1=1 AND in a SQL clause? That question isn't an answer to this question. Where-clause: select * from table where 1=1 and sStatus not in ('status1','status2','status3') No programming or if statements to push an and in there. A straight query. If you could un-close this, I would like to know whether there is a purpose so that I may rewrite and remove the 1=1 if it is unnecessary.

Conditional WHERE clause with CASE statement in Oracle

耗尽温柔 提交于 2019-11-28 09:42:14
I'm brand-new to the Oracle world so this could be a softball. In working with an SSRS report, I'm passing in a string of states to a view. The twist is that the users could also pick a selection from the state list called "[ No Selection ]" ... (that part was not by doing and I'm stuck with implementing things this way) If they choose the No Selection option, then I just want to return all states by default, otherwise return just the list of states that are in my comma-separated list. This really seems like it should be easy but I'm stuck. Here is the code I have so far (just trying to get a

SQL Server: How to use UNION with two queries that BOTH have a WHERE clause?

三世轮回 提交于 2019-11-28 06:20:55
Given: Two queries that require filtering: select top 2 t1.ID, t1.ReceivedDate from Table t1 where t1.Type = 'TYPE_1' order by t1.ReceivedDate desc And: select top 2 t2.ID from Table t2 where t2.Type = 'TYPE_2' order by t2.ReceivedDate desc Separately, these return the ID s I'm looking for: (13, 11 and 12, 6) Basically, I want the two most recent records for two specific types of data. I want to union these two queries together like so: select top 2 t1.ID, t2.ReceivedDate from Table t1 where t1.Type = 'TYPE_1' order by ReceivedDate desc union select top 2 t2.ID from Table t2 where t2.Type =

Selecting rows where remainder (modulo) is 1 after division by 2?

怎甘沉沦 提交于 2019-11-28 04:50:33
There is a column in options that hold an integer. I want to select the row only if that value % 2 = 1. I know this can be done in 2 queries but is it possible to do it in 1? MySQL, SQL Server, PostgreSQL, SQLite support using the percent sign as the modulus: WHERE column % 2 = 1 For Oracle, you have to use the MOD function : WHERE MOD(column, 2) = 1 At least some versions of SQL (Oracle, Informix, DB2, ISO Standard) support: WHERE MOD(value, 2) = 1 MySQL supports '%' as the modulus operator: WHERE value % 2 = 1 select * from table where value % 2 = 1 works fine in mysql. Note: Disregard this

How to construct Where Expression dynamically in Entity Framework?

核能气质少年 提交于 2019-11-28 03:41:24
问题 I've taken a look at this answer on how to dynamically create an OrderBy expression in Entity Framework. But I'd like to also build a dynamic Where expression. Something along the lines of this: public IEnumerable<InventoryItem> GetAll(string filterBy, object value) { var results = new List<InventoryItem>(); using (var db = new InventoryDb()) { if (QueryHelper.PropertyExists<InventoryItem>(filterBy)) { var query = db.rri_InventoryItems.WhereByProperty(filterBy, value); foreach(var item in

Ignore particular WHERE criteria

被刻印的时光 ゝ 提交于 2019-11-28 01:13:05
问题 I want to execute a parameterized query to perform a search by user-supplied parameters. There are quite a few parameters and not all of them are going to be supplied all the time. How can I make a standard query that specifies all possible parameters, but ignore some of these parameters if the user didn't choose a meaningful parameter value? Here's an imaginary example to illustrate what I'm going for $sql = 'SELECT * FROM people WHERE first_name = :first_name AND last_name = :last_name AND

Conditional Where clauses in JasperReports

狂风中的少年 提交于 2019-11-28 00:34:18
问题 Let's say I want a JasperReport that lets the user filter on a date if they so wish. The SQL is as follows: select * from foo where bar = $P{bar} and some_date > $P{some.date} Now, I don't want to filter by some date if they didn't pass the date in. I found the following kludge that people use: select * from foo where bar = $P{bar} $P!{some.date.fragment} And the some.date.fragment parameter is defined with the following default: ($P{some.date} == null || $P{some.date}.equals("")) ? "" : "AND

Can someone recommend a good tutorial on MySQL indexes, specifically when used in an order by clause during a join? [closed]

主宰稳场 提交于 2019-11-27 22:42:39
问题 I could try to post and explain the exact query I'm trying to run, but I'm going by the old adage of, "give a man a fish and he'll eat for a day, teach a man to fish and he'll eat for the rest of his life." SQL optimization seems to be very query-specific, and even if you could solve this one particular query for me, I'm going to have to write many more queries in the future, and I'd like to be educated on how indexes work in general. Still, here's a quick description of my current problem. I

MySQL Multiple Where Clause

浪子不回头ぞ 提交于 2019-11-27 19:43:43
I have a table like this: id image_id style_id style_value ----------------------------------- 1 45 24 red 1 45 25 big 1 47 26 small 1 45 27 round 1 49 28 rect I want to take image_id column if: style_id = 24 and style_value = red style_id = 25 and style_value = big style_id = 26 and style_value = round I have make a query like this: $query = mysql_query("SELECT image_id FROM list WHERE (style_id = 24 AND style_value = 'red') AND (style_id = 25 AND style_value = 'big') AND (style_id = 27 AND style_value = 'round') But i couldn't get any result. When i make this sample with OR, it works well.

Oracle date “Between” Query

帅比萌擦擦* 提交于 2019-11-27 18:27:58
I am using oracle database. i want to execute one query to check the data between two dates. NAME START_DATE ------------- ------------- Small Widget 15-JAN-10 04.25.32.000000 PM Product 1 17-JAN-10 04.31.32.000000 PM select * from <TABLENAME> where start_date BETWEEN '15-JAN-10' AND '17-JAN-10' But I dont get any results from above query. I think I have to use "like" and "%". But I dont know where to use them. Please throw some lights on this. thanks in advance. Judging from your output it looks like you have defined START_DATE as a timestamp. If it were a regular date Oracle would be able to