Sqlite if it contains

Given the database field named "widget_ids" containing data such as "67/797/124 /" or "45 /", where the numbers are separated by the widget_ids character ... how would you make an expression about updating with SQL that says : "if the widget_ids of the line with id X contains the text" somenumber / "do nothing, otherwise add" somenumber / "to the current value"

Can you do something similar with SQL or, more specifically, sqlite? Is this something that is better done in the program for some reason, or is there if-then syntax support in SQL?

+3
source share
2 answers

if-thens, if-then SQL. :

update <tablename>
  set widget_id = widget_id + "somenumber/"
  where row_id = X
    and widget_id not like "%/somenumber/%"
    and widget_id not like "somenumber/%";
+7

, . .

CREATE TABLE ThingieWidgets (
  thingie_id INT REFERENCES Thingies,
  widget_id INT REFERENCES Widgets,
  PRIMARY KEY(thingie_id, widget_id)
);

, :

INSERT INTO ThingieWidgets (thingie_id, widget_id)
  VALUES (1234, 67), (1234, 797), (1234, 124);

, Thingie 1234 45:

SELECT * FROM ThingieWidgets
WHERE thingie_id = 1234 AND widget_id = 45;

, :

INSERT INTO ThingieWidgets (thingie_id, widget_id)
  VALUES (1234, 45);
+3

Source: https://habr.com/ru/post/1697698/


All Articles