between

MySQL UPDATE with random number between 1-3

邮差的信 提交于 2019-11-28 06:13:05
Got a big table and I want to add a column that has a randomly chosen number for each record. 1, 2, or 3. Having a hard time. Any ideas? Rabbie Try this: UPDATE tableName SET columnName = FLOOR( 1 + RAND( ) *3 ); From the MySQL documentation for RAND : Returns a random floating-point value v in the range 0 <= v < 1.0. So in the above query, the largest value which could be generated by 1 + RAND()*3 would be 3.999999 , which when floored would give 3. The smallest value would occur when RAND() returns 0, in which case this would give 1. Faisal Use RAND() function. It returns a random floating

Postgresql query between date ranges

二次信任 提交于 2019-11-28 03:17:32
I am trying to query my postgresql db to return results where a date is in certain month and year. In other words I would like all the values for a month-year. The only way i've been able to do it so far is like this: SELECT user_id FROM user_logs WHERE login_date BETWEEN '2014-02-01' AND '2014-02-28' Problem with this is that I have to calculate the first date and last date before querying the table. Is there a simpler way to do this? Thanks With dates (and times) many things become simpler if you use >= start AND < end . For example: SELECT user_id FROM user_logs WHERE login_date >= '2014-02

Why is Rails is adding `OR 1=0` to queries using the where clause hash syntax with a range?

会有一股神秘感。 提交于 2019-11-27 23:09:50
问题 The project that I'm working on is using MySQL on RDS (mysql2 gem specifically). When I use a hash of conditions including a range in a where statement I'm getting a bit of an odd addition to my query. User.where(id: [1..5]) and User.where(id: [1...5]) Result in the following queries respectively: SELECT `users`.* FROM `users` WHERE ((`users`.`id` BETWEEN 1 AND 5 OR 1=0)) SELECT `users`.* FROM `users` WHERE ((`users`.`id` >= 1 AND `users`.`id` < 5 OR 1=0)) The queries work perfectly fine

SQL BETWEEN for text vs numeric values

流过昼夜 提交于 2019-11-27 17:58:41
问题 BETWEEN is used in a WHERE clause to select a range of data between two values. If I am correct whether the range's endpoint are excluded or not is DBMS specific. What I can not understand in the following: If I have a table of values and I do the following query: SELECT food_name FROM health_foods WHERE calories BETWEEN 33 AND 135;` The query returns as results rows including calories =33 and calories =135 (i.e. range endpoints are included ). But if I do: SELECT food_name FROM health_foods

T-SQL Between Dates Confusion

青春壹個敷衍的年華 提交于 2019-11-27 15:12:07
I am working with T-SQL in SQL Server 2000 and I have a table TRANSACTIONS which has a date column TRANDATE defined as DateTime, among many other columns which are irrelevant for this question.. The table is populated with transactions spanning many years. I ran into code, test, that has me confused. There is a simple SELECT , like this: SELECT TRANDATE, RECEIPTNUMBER FROM TRANSACTIONS WHERE TRANDATE BETWEEN '12/01/2010' and '12/31/2010' ORDER BY TRANDATE and its not returning two rows of data that I know are in that table. With the statement above, the last row its returning, in order, has a

PostgreSQL index not used for query on range

让人想犯罪 __ 提交于 2019-11-27 13:15:45
问题 I'm using PostgreSQL (9.2.0) and have a table of IP ranges. Here's the SQL: CREATE TABLE ips ( id serial NOT NULL, begin_ip_num bigint, end_ip_num bigint, country_name character varying(255), CONSTRAINT ips_pkey PRIMARY KEY (id ) ) I've added indices on both begin_ip_num and end_ip_num : CREATE INDEX index_ips_on_begin_ip_num ON ips USING btree (begin_ip_num ); CREATE INDEX index_ips_on_end_ip_num ON ips USING btree (end_ip_num ); The Query being used is: SELECT "ips".* FROM "ips" WHERE

What's mysql's “BETWEEN” performance over..?

a 夏天 提交于 2019-11-27 08:33:39
Is there any better performance when querying in (particularly) mysql of the following: SELECT * FROM `table` WHERE `unix_date` BETWEEN 1291736700 AND 1291737300 over: SELECT * FROM `table` WHERE `unix_date` >= 1291736700 AND `unix_date` <= 1291737300 or BETWEEN syntax is just being substituted with the second sql? As I recall, there's no difference. But see for yourself if: explain plan for SELECT * FROM `table` WHERE `unix_date` BETWEEN 1291736700 AND 1291737300 and: explain plan for SELECT * FROM `table` WHERE `unix_date` >= 1291736700 AND `unix_date` <= 1291737300 produce the same plans.

Check if current date is between two dates Oracle SQL

允我心安 提交于 2019-11-27 07:26:11
I would like to select 1 if current date falls between 2 dates through Oracle SQL. I wrote an SQL after reading through other questions. https://stackoverflow.com/questions/2369222/oracle-date-between-query https://stackoverflow.com/questions/2399753/select-from-table-by-knowing-only-date-without-time-oracle But it returned only null. sysdate is the current date that is 01/05/2014 in date format DD/MM/YYYY . The SQL I wrote is: select 1 from dual WHERE to_date(sysdate,'DD/MM/YYYY') BETWEEN TO_DATE('28/02/2014', 'DD/MM/YYYY') AND TO_DATE('20/06/2014', 'DD/MM/YYYY'); and select 1 from dual WHERE

Date range in date range

℡╲_俬逩灬. 提交于 2019-11-27 05:13:25
Actually this task seemed very easy to me, but i got a little bit stuck and would be thankful for some hints :D I have some events with a start and an end time - and i would like to create a table with calendar weeks. Therefore i wrote a method to to check if an event is within this week to color it like this: private boolean inWeek(Date date, Entry pe) { return ((pe.getStartsAt().after(Util.firstDayOfWeek(date)) || pe.getStartsAt().equals(Util.firstDayOfWeek(date))) && (pe.getEndsAt().before(Util.lastDayOfWeek(date)) || pe.getEndsAt().equals(Util.lastDayOfWeek(date)))); } This case was okay

Select all months within given date span, including the ones with 0 values

有些话、适合烂在心里 提交于 2019-11-26 22:59:35
I'm trying to write a MySQL query to get an average value per month, for all months between to given dates. My idea is this: Query, something like SELECT AVG(value1) as avg_value_1, AVG(value2) as avg_value_2, MONTH(save_date) as month, YEAR(save_date) as year FROM myTable WHERE save_date BETWEEN '2009-01-01' AND '2009-07-01' GROUP BY YEAR(save_date), MONTH(save_date) avg_value_1 | avg_value_2 | month | year 5 | 4 | 1 | 2009 2 | 1 | 2 | 2009 7 | 5 | 3 | 2009 0 | 0 | 4 | 2009 <--- 6 | 5 | 5 | 2009 3 | 6 | 6 | 2009 You see, no values were entered during April 2009, yet i want it to show up as a