sql

Migrating an Oracle MERGE statement to a PostgreSQL UPSERT statement

社会主义新天地 提交于 2021-02-18 15:22:42
问题 Can you please help me converting the following Oracle MERGE statement into a valid UPSERT statement for use in a PostgreSQL 9.3 database? MERGE INTO my_table a USING (SELECT v_c1 key, v_c2 AS pkey, v_c3 AS wcount, v_c4 AS dcount FROM DUAL) b ON ( a.key = b.key AND a.pkey = b.pkey WHEN MATCHED THEN UPDATE SET wcount = b.wcount, dcount = b.dcount WHEN NOT MATCHED THEN INSERT (key, pkey, wcount, dcount) VALUES(b.key,b.pkey,b.wcount,b.dcount); 回答1: I don't think there's UPSERT statement in

Laravel 4 Eloquent returns wrong ID

有些话、适合烂在心里 提交于 2021-02-18 12:04:27
问题 I have 3 tables in my database: Campaigns Users Companies One company may have some users. One user may have some campaigns. A user (with admin rights) can do some actions with any campaign that belongs to his company. So, I want to check whether he's doing these actions with his campaign or not (in the last case I return something like "access denied"). My condition Campaign::join('users', 'users.id', '=', 'campaigns.user_id') ->where('users.company_id', '=', Auth::user()->company->id) -

Laravel 4 Eloquent returns wrong ID

为君一笑 提交于 2021-02-18 12:03:45
问题 I have 3 tables in my database: Campaigns Users Companies One company may have some users. One user may have some campaigns. A user (with admin rights) can do some actions with any campaign that belongs to his company. So, I want to check whether he's doing these actions with his campaign or not (in the last case I return something like "access denied"). My condition Campaign::join('users', 'users.id', '=', 'campaigns.user_id') ->where('users.company_id', '=', Auth::user()->company->id) -

Room: could I check passing value to query for NULL?

﹥>﹥吖頭↗ 提交于 2021-02-18 11:38:30
问题 I'm using Room libs, have DB Stations and want to execute query to get Stations with/without filter. UPD. I have DAO and I want to get all the records when my array (groupIds) is null or empty and get filtered list if I have at list one element in array. @Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE ((:groupIds) IS NULL OR $GROUP_ID IN(:groupIds)) fun getStationsWithFilter(groupIds: IntArray?): Flowable<List<DbStation>> At this moment I had an issue 08-29 16:09:00.139 9508-9508/-

Find gaps of a sequence in SQL without creating additional tables

老子叫甜甜 提交于 2021-02-18 11:29:14
问题 I have a table invoices with a field invoice_number . This is what happens when i execute select invoice_number from invoice : invoice_number -------------- 1 2 3 5 6 10 11 I want a SQL that gives me the following result: gap_start | gap_end 4 | 4 7 | 9 How can i write a SQL to perform such query? I am using PostgreSQL. 回答1: With modern SQL, this can easily be done using window functions: select invoice_number + 1 as gap_start, next_nr - 1 as gap_end from ( select invoice_number, lead(invoice

Error: invalid input syntax for integer: “”

£可爱£侵袭症+ 提交于 2021-02-18 11:12:14
问题 I have this table tbl_buku : id_buku judul_buku tahun_buku 1 Bioogi 2010 2 Fisika 2010 3 Informatika 2012 4 Kimia 2012 I use query like this, but I am getting an error: select case when t1.tahun_buku=t2.tahun_buku then '' else t1.tahun_buku end tahun_buku,t1.judul_buku from tbl_buku t1 left join tbl_buku t2 on t1.id_buku-1=t2.id_buku; I want to show table like this: tahun_buku judul_buku 2010 Biologi Fisika 2012 Informatika Kimia How to achieve this? 回答1: I think the problem in your query is

Error: invalid input syntax for integer: “”

為{幸葍}努か 提交于 2021-02-18 11:12:07
问题 I have this table tbl_buku : id_buku judul_buku tahun_buku 1 Bioogi 2010 2 Fisika 2010 3 Informatika 2012 4 Kimia 2012 I use query like this, but I am getting an error: select case when t1.tahun_buku=t2.tahun_buku then '' else t1.tahun_buku end tahun_buku,t1.judul_buku from tbl_buku t1 left join tbl_buku t2 on t1.id_buku-1=t2.id_buku; I want to show table like this: tahun_buku judul_buku 2010 Biologi Fisika 2012 Informatika Kimia How to achieve this? 回答1: I think the problem in your query is

How to limit the maximum display length of a column in PostgreSQL

随声附和 提交于 2021-02-18 10:25:32
问题 I am using PostgreSQL, and I have a column in a table which contains very long text. I want to select this column in a query, but limit its display length. Something like: select longcolumn (only 10 chars) from mytable; How would I do this? 回答1: What you can do is use PostgreSQL's substring() method. Either one of the two commands below will work: SELECT substring(longcolumn for 10) FROM mytable; SELECT substring(longcolumn from 1 for 10) FROM mytable; 回答2: Slightly shorter: SELECT left

Get row index in datatable from a certain column

痴心易碎 提交于 2021-02-18 09:54:51
问题 | 1 | 2 | 3 | +------------+ | A | B | C | | D | E | F | | G | H | I | System.Data.DataTable dt = new DataTable(); dt.Columns.Add("1"); dt.Columns.Add("2"); dt.Columns.Add("3"); dt.Rows.Add(new object[] { "A", "B", "C" }); dt.Rows.Add(new object[] { "D", "E", "F" }); dt.Rows.Add(new object[] { "G", "H", "I" }); int? index = null; var rows = new System.Data.DataView(dt).ToTable(false, new[] {"1"}).Rows; for (var i = 0; i < rows.Count; i++) { if (rows[i].ItemArray.FirstOrDefault() as string ==

Sql Server union with If condition

♀尐吖头ヾ 提交于 2021-02-18 09:33:52
问题 I have a query like: DECLARE @tmpValue SET @tmpValue = 0 -- it will be change SELECT * FROM Animal WHERE AniActive = 1 UNION IF @tmpValue > 0 SELECT * FROM Animal WHERE.Active = 0 When I use like this it is giving error because of if condition. I have to use UNION because of our structure. How can I use it with if condition? Thanks, John 回答1: Move the condition @tmpValue > 0 to the WHERE clause like so: SELECT * FROM Animal WHERE AniActive = 1 UNION SELECT * FROM Animal WHERE @tmpValue > 0