not-exists

PostgreSQL create table if not exists

霸气de小男生 提交于 2019-11-26 19:47:50
In a MySQL script you can write: CREATE TABLE IF NOT EXISTS foo ...; ... other stuff ... and then you can run the script many times without re-creating the table. How do you do this in PostgreSQL? This feature has been implemented in Postgres 9.1 : CREATE TABLE IF NOT EXISTS myschema.mytable (i integer); For older versions , here is a function to work around it: CREATE OR REPLACE FUNCTION create_mytable () RETURNS void AS $func$ BEGIN IF EXISTS (SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'myschema' AND tablename = 'mytable') THEN RAISE NOTICE 'Table myschema.mytable already exists.'

Array.push() if does not exist?

走远了吗. 提交于 2019-11-26 11:39:41
How can I push into an array if neither values exist? Here is my array: [ { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" } ] If I tried to push again into the array with either name: "tom" or text: "tasty" , I don't want anything to happen... but if neither of those are there then I want it to .push() How can I do this? Darin Dimitrov You could extend the Array prototype with a custom method: // check if an element exists in array using a comparer function // comparer : function

MySQL monthly Sale of last 12 months including months with no Sale

五迷三道 提交于 2019-11-26 08:19:31
问题 SELECT DATE_FORMAT(date, \"%b\") AS month, SUM(total_price) as total FROM cart WHERE date <= NOW() and date >= Date_add(Now(),interval - 12 month) GROUP BY DATE_FORMAT(date, \"%m-%Y\") This query displaying result for only existing month. I need all 12 months sales. Output : \"month\" \"total\" -------------- \"Jun\" \"22\" \"Aug\" \"30\" \"Oct\" \"19\" \"Nov\" \"123\" \"Dec\" \"410\" Required Output : \"month\" \"total\" -------------- \"Jan\" \"0\" \"Feb\" \"0\" \"Mar\" \"0\" \"Apr\" \"0\"