case

MySQL update conditions in one query (UPDATE, SET & CASE)

天大地大妈咪最大 提交于 2019-12-21 03:44:14
问题 I'm trying to update a set of records ( boolean fields ) in a single query if possible. The input is coming from paginated radio controls, so a given POST will have the page's worth of IDs with a true or false value. I was trying to go this direction: UPDATE my_table SET field = CASE WHEN id IN (/* true ids */) THEN TRUE WHEN id IN (/* false ids */) THEN FALSE END But this resulted in the "true id" rows being updated to true , and ALL other rows were updated to false . I assume I've made some

Mysql case not working

纵饮孤独 提交于 2019-12-20 07:38:55
问题 SELECT SQL_CALC_FOUND_ROWS a.* , zn.`name` AS zone_name, c.`name` AS carrier_name, CASE type WHEN type=1 THEN 'General day' ELSE 'Special date' END AS type_changed, CASE week_day WHEN week_day = -1 THEN 'notset' WHEN week_day = 1 THEN 'monday' WHEN week_day = 2 THEN 'tuesday' WHEN week_day = 3 THEN 'wednesday' WHEN week_day = 4 THEN 'thursday' WHEN week_day = 5 THEN 'friday' WHEN week_day = 6 THEN 'saturday' WHEN week_day = 7 THEN 'sunday' END AS week_day_mod , IF(date = '0001-01-01 00:00:0',

Average and Case in SQL

佐手、 提交于 2019-12-20 05:59:13
问题 I am trying to generate a report and the following code does not produce the desired results which gives me 2 lines rather than one. The ScoreTypeID could have values of 22, 52, 3 or 4 . if it is 22 or 52, I need the average and if not I need to show 0. Any idea what may be the problem ? Thanks. CASE WHEN FAS1.ScoreTypeID = 22 THEN avg(fas1.totalscore) WHEN FAS1.ScoreTypeID = 52 THEN avg(fas1.totalscore) ELSE 0 END AS 'Total Score', 回答1: I think in your full query, you are missing the GROUP

Attendance Report in MySql

荒凉一梦 提交于 2019-12-20 05:23:17
问题 I want to write a query to generate attendance report of employee. First I will tell you how the presence of employee is stored in my database. I have following tables. Employee Table with Columns emp_id emp_Name Joining_Date 1 john 11-01-2012 2 Scott 12-01-2012 Holiday Table Holiday_Name Date Chrismas 25-12-2012 Dushera 08-03-2012 Independance Day 15-08-2012 Leave Table Subject from_Date to_Date Emp_Id status PL 02-01-2012 04-01-2012 1 Approved CL 11-01-2012 12-01-2012 2 Declined Doctor

CASE in WHERE CLAUSE in MYSQL

点点圈 提交于 2019-12-20 03:32:50
问题 The question is as simple as the title says,But here is one logic. Here is my code CREATE TABLE `inf_brand_images` ( `id` bigint(99) NOT NULL AUTO_INCREMENT, `brand` varchar(255) NOT NULL, `thumb` text NOT NULL, `is_active` int(2) NOT NULL DEFAULT '1', `cmp_brand` varchar(1024) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=6458 DEFAULT CHARSET=latin1 Here is data in this table ID | brand | thumb |is_active| cmp_brand 1 | NIKE | a.png | 1 | 2 | DUNHILL| b.png | 1 | 3 | NIKE | c

SQL Case Order By specific order

谁说胖子不能爱 提交于 2019-12-20 02:45:20
问题 Ok, I asked something similar before, but I've researched it and haven't found this specifically. I have a table that I need sorted on fields OptionName(NVarChar) and IsActive(BIT). I need the results to be in the following order for a DDL: Option A Option B Option C Options that are Active, by OptionName ASC Option D Options that are Inactive, by OptionName ASC So far I have ORDER BY CASE WHEN PortalName = 'Company, Inc' THEN 0 ELSE 1 END, CASE WHEN PortalName = 'Setup' THEN 1 ELSE 2 END,

Conditional in MYSQL where clause

人盡茶涼 提交于 2019-12-19 07:51:22
问题 I have a query that if a flag called OrderBy is 1 then it is a calendar event and I need to check for a range between two date time fields. Whereas all other types we just check for the day you are viewing. I research if statements and saw many posts where it was suggested a case be used so I tried to implement it into my query. My query needs the condition in the where. I know I have syntax issues and that is why I am here in the hopes that someone could point out the correct way to do this.

Codeigniter : Error in ORDER BY CASE query

蹲街弑〆低调 提交于 2019-12-19 02:37:13
问题 Here is my query in Codeigniter $this->db->select('p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value',FALSE); $this->db->from($this->_tbl_projects . ' as p'); $this->db->join($this->_tbl_client_details . ' as c', 'c.id = p.client_id', 'left'); $this->db->join($this->_tbl_users . ' as u', 'u.id = c.user_id', 'left'); $this->db->join($this->_tbl_project_types . ' as pt', 'pt.project_type_id = p.project_type_id', 'left'); $this->db->join($this->_tbl_specializations

ORDER BY AND CASE IN SQL SERVER

若如初见. 提交于 2019-12-18 13:37:23
问题 I need to have an order by functionality inside a stored procedure. A value is posted to a webservice and based on that value I have to order the results in a certain way i.e. When ColName is posted order by ColName When ColName2 is posted order by ColName2 I was looking into using Case but I am getting an error: Incorrect syntax near '@version' ORDER BY CASE WHEN @OrderBy ='Seller (code)' THEN A_SNO WHEN @OrderBy ='Lot' THEN A_LOTNO WHEN @OrderBy ='Ring Type' THEN RN_NUM WHEN @OrderBy ='Aim

Show empty string when date field is 1/1/1900

你。 提交于 2019-12-18 13:06:10
问题 I'm querying a database like so: SELECT DISTINCT CASE WHEN CreatedDate = '1900-01-01 00:00:00.000' THEN '' ELSE CreatedDate END AS CreatedDate FROM LitHoldDetails lhd.CreatedDate is a DateTime field and is non-nullable. I want to display an empty string if the field is the minimum date (1/1/1900), but my CASE statement doesn't work; CreatedDate displays 1900-01-01 00:00:00.000 in my query when that value is in the database. I'm using SQL Server 2008 R2. What am I doing wrong? 回答1: When you