2

I have come across quite a few questions reguarding SQL SELECT IF EXISTS but I am unable to piece together exactly what I need.

What I am trying to do is select specific data from a row in one table if that row exists, otherwise create the row with default values, and do it all in one query to the database.

I am able to create the row in the database if it does not already exist, and if it does exist then select the data from the row, but I want to combine the latter two if at all possible.

So, instead of this: 1, check if row exists using SELECT 2, if row does not exist INSERT 3, if row does exist SELECT again to grab data

I would like to do: 1, check if row exists using select, if row does exist grab the data all in the same query 2, if row does not exist insert row

I hope I explained it properly, and thank you all for the assistance it is greatly appreciated!

eggyal
  • 113,121
  • 18
  • 188
  • 221
KKlouzal
  • 642
  • 5
  • 25
  • 1
    you may be use stored procedure – shola Dec 27 '13 at 11:28
  • Do you want to update record if already exists? – shola Dec 27 '13 at 11:36
  • you can go for insert if not exist if you want update and refer record which updated please refer :http://stackoverflow.com/questions/3164505/mysql-insert-record-if-not-exists-in-table – shola Dec 27 '13 at 11:49
  • I do not want to update the record if it already exists, I want to load it, but thats not the issue, the issue is I dont want to use two select calls, I want to check if the data exists AND grab it all in the same select function. Not sure if this is even possible. – KKlouzal Dec 27 '13 at 14:43

2 Answers2

1

Not entirely sure what the problem is here. Surely all you want (in pseudocode) is:

qryexecute("SELECT * FROM table WHERE ... FOR UPDATE")

if numrows(qry) > 0 then
  rowfetchdata(qry)
else
  execute("INSERT INTO table VALUES (...)")
  row ← ... // inserted data
end if

// use row
eggyal
  • 113,121
  • 18
  • 188
  • 221
  • Yes I believe this is what I want, to not have to use two select calls, be able to check if the row exists AND grab the data all in the same select call. :) – KKlouzal Dec 27 '13 at 14:45
  • @user3052750: So, what's the problem? – eggyal Dec 27 '13 at 15:10
  • Your psudocode makes sense, I dont know how to store a querey in a variable to use it twice :) – KKlouzal Dec 27 '13 at 16:10
  • @user3052750: That is a problem that has very little to do with MySQL and a great deal to do with the code through which you access it. What language/libraries/frameworks/tools are you using? – eggyal Dec 27 '13 at 16:58
  • I'm using lua, which allows the full range of sql syntax, I just don't know if theres a way to create a variable with sql that will hold the select query, if so thats exactly what I need! – KKlouzal Dec 27 '13 at 18:36
  • @user3052750: This has nothing to do with SQL syntax, least of all "*creating a variable with SQL*". I don't know lua, but it is within *an lua variable* that you need to store your resultset. – eggyal Dec 27 '13 at 18:42
0
IF Not EXISTS (SELECT * FROM ------)
BEGIN
    --Insert HERE
END
ELSE
BEGIN
   --  select HERE
END
Nagaraj S
  • 12,563
  • 6
  • 30
  • 50