« first day (1295 days earlier)      last day (1363 days later) » 

4:27 PM
Hey, anybody home?
 
4:52 PM
@KristianVitozev yup! As the topic says, Ask your question, and then hang around a while to see if an expert looks back at their screen and answers it!
 
5:03 PM
any one can help me
if exists (select phone_number from pos_customer where phone_number = '9039935222')
begin
select * from pos_customer where phone_number = '9039935222'
end
else
begin
select * from pos_customer
end
it's giving me an error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists (select phone_number from pos_customer where phone_number = '903993522' at line
 
 
2 hours later…
6:34 PM
@anurag that syntax is only valid in the context of a stored procedure or function
you want a SELECT query that returns the matching customer if it exists, and otherwise returns all customers?
Why wouldn't you do that business logic in your app? Why do it in the database?
It is possible to do that in a single SELECT query, but I don't think you would want to
 
actually i want single trip to DB
well thanks
for valuable info
 
m59
@TehShrike hey bro. I've been so busy - I haven't had time to check out that group
 
@anurag what's your latency? I don't think that should really be your concern
how will your app know the difference between the phone number matching two users, and the customer table only having two rows?
 
phone number is unique in the DB
then we can
find out
 
I would strongly recommend that you just run two queries, for the simplicity of your app
but it is possible to do in a single SELECT
 
m59
6:42 PM
How do I do something like this if the record might not exist?
         UPDATE foo SET foo.bar = ?
         WHERE foo.baz = ?
 
@m59 which group?
@m59 INSERT ... ON DUPLICATE KEY UPDATE is the traditional MySQL way
 
m59
XMPP
 
oh yeah!
 
m59
Ah, I should've known that :) Thanks
 
@te
@TehShrike
would you suggest me how can achieve it
 
6:46 PM
@anurag I would suggest having one query server-side to "get me the customer whose phone number matches this number" and then have another query to "get all customers"
if the first query returns 0 rows, run the second query
 
if phone no exist then update else insert
 
unless you're writing a client that is reaching a database somewhere out on the internet, it's not worth the confusing code/queries you'd have to write to pile it into a single request
@anurag that's a bit different from your original question, but as with m59's question, you could use INSERT ... ON DUPLICATE KEY UPDATE
 
yes i am agree with you
sorry for that
ultimateely we want to update if record exist else insert
and how to manage else part
set @var= (select Count(*) from pos_customer where phone_number= '9039935222');

if @var > 0
then

select 'update';
else
select 'insert';
end if;
Error :#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if @var > 0
then

select 'update'' at line 1
 
@anurag in your code, you should select id from pos_customer where phone_number= '9039935222'
if it returns an id, then you run an UPDATE query to update the record with that id
if it returns no rows, then you run an INSERT query
 
7:02 PM
so we have to call three times
?
 
@anurag twice. Once to see if the record exists, and another time to either update or insert the record.
I think you're overestimating the expense of a query. Where is the client running, and where is the server running?
 
ok thanks i got you
we can't fire in one time like MS SQL
thanks
 
m59
INSERT INTO foo(
  foo.data,
  foo.bar_id
) VALUES(?,?)
JOIN bar ON bar.id = ?
ON DUPLICATE KEY UPDATE foo.data = ?
WHERE bar.owner.id = ?
I didn't imagine that was going to work...
 
thanks
 
m59
bar.owner_id ***
I want to make sure the query will fail if a related column doesn't match the userId parameter
 
7:56 PM
@m59 ah, so you want to create a foreign key relationship between bar.owner_id and your user table
that's a thing that MySQL (InnoDB) has built-in
 
m59
Oh screw. Yeah I have that.
I'm a dope.
I changed the schema in my install script and forgot to run it
/fired
lol
Mental jobs are weird.
 
heehee
 
m59
8:14 PM
This seems to work now
INSERT INTO foo(
  foo.data,
  foo.bar_id
)
SELECT ?, bar.id FROM bar
WHERE
bar.id = ? AND
bar.owner_id = ?
ON DUPLICATE KEY UPDATE foo.data = ?
the previous one still had a syntax error. Is ^ that decent?
 
8:41 PM
@m59 what's the syntax error?
it looks reasonable, though I don't think with prepared statements you can usually put a parameter in the SELECT portion of the query
 
m59
Actually, I was saying this one worked.
The previous one up there is the one that threw the error... it was just generic.
 
ah, ok
yeah, that looks reasonable to me
 

« first day (1295 days earlier)      last day (1363 days later) »