473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

MYSQL if statement syntax

Hi,
currently im using MYSQL workbench to extract some features needed for my project. However im having a problem with this query,

select newtime from new_event
IF newtime < 80101
than set newtime = 0
elseif newtime > 80000 and newtime < 160101 than set newtime = 1
elseif newtime >160000 than set newtime = 2
end if

I have converted the time into numeric; hence newtime. Im trying to use newtime to create a time frame ( 0 , 1 , 2) . Unfortunately when i compiled it keeps giving an error code 1064

new_event is a new table created while doing the first initial query.

I hope you could really help me.

thank u
Dec 9 '11 #1
6 4508
Rabbit
12,516 Expert Mod 8TB
Is this supposed to be SQL code? Because the first line looks like SQL but the rest of it is not.
Dec 9 '11 #2
hi,
i tried something else, but it still gives the same error.


DELIMITER $$

select * from new_event where for each row
begin
set a = select newtime from new_event where cid
if a < 80101 then set newtime = 0
else set newtime = 1

end if

DELIMITER;

can you help me with the syntax.

thank u.
Dec 10 '11 #3
Rabbit
12,516 Expert Mod 8TB
I'm not exactly sure what it is you're trying to do. I don't know if you're using just SQL or not. And I have no idea if you're trying to run an update or if you're just trying to calculate a field.
Dec 11 '11 #4
first and foremost , yes im using SQL. only SQL query language. I am using MYSQL workbench to extract some features needed for my project. I have managed to extract the features needed and store it in a new table call new event. I have also converted the time into integer. Below is the sql code used :-

select iphdr.cid ,iphdr.ip_dst, date(event.timestamp), convert(time(event.timestamp), unsigned) as newtime, event.signature
from iphdr, event
where iphdr.cid = event.cid
order by event.signature;


result (some of the output)

cid | ip_add | date | time | signature

480 | 3232284675 | 2011-11-19 | 4328 | 1

482 | 3232284675 | 2011-11-19 | 4328 | 1

1 | 3232284675 | 2011-11-19 | 113928 | 1

2 | 3232284675 | 2011-11-19 | 235959 | 2


Since I have converted the time into numeric; hence newtime, now i need to segregate the output form the query above into three different time frame (time frame 0 , 1 and 2). Here is the problem .... when i try to do it using the codes i posted previously i keep getting the 1064 error code. I thought that by using IF then Else , i could easily separate the data into 3 different time frame.
The output i need is like below

cid | ip_add | date |time | sig.|timeFrame

480 | 3232284675 |2011-11-19 |4328 | 1 | 0

482 | 3232284675 |2011-11-19 |4328 | 1 | 0

1 | 3232284675 |2011-11-19 |113928 | 1 | 1

2 | 3232284675 |2011-11-19 |235959 | 2 | 2

the code i used earlier (with 1064 error code) was suppose to do that.

I hope im making more sense now. Im a complete klutz when it comes to programming. im sorry .
Im also trying to make this work by browsing the net. Ill be working hard while waiting for your reply.

thank you .
Dec 11 '11 #5
Rabbit
12,516 Expert Mod 8TB
What you're doing is basically trying to create a calculated column. It needs to be within the select clause, not out of it. Also, use CASE rather than IF. And you're spelling THEN incorrectly. And finally, you can't use SET, that is used for variables.

Expand|Select|Wrap|Line Numbers
  1. select 
  2.    case when newtime < 80101
  3.       then 0
  4.    when newtime between 80101 and 160100
  5.       then 1
  6.    when newtime > 160100
  7.       then 2
  8.    end as timesegment
  9. from new_event
Dec 11 '11 #6
thank you so very much.
Dec 12 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Julien - Marseille | last post by:
Hello, I need help for php syntax when i call Mysql database I have wrote that and my sql connection is working I just have a problem with this command line : $query = "SELECT * FROM...
0
by: Jonas Gorauskas | last post by:
I am having this one problem on an app I am writing where it shows the following error on some error trapping that I did: Query failed: Error Number: 1064 Description: You have an error in...
4
by: Roy Epperson | last post by:
I'm not finding what the maximum number of characters that a statement can be. Anyone know where I can find that spec?
4
by: gregpinero | last post by:
It's the strangest thing, I'm pulling some text out of a MySQL table and trying to run exec on it, and it keeps giving me a syntax error, always at the end of the first line. Thanks in advance...
5
by: jjeanj1 | last post by:
I am having issue using a user defined variable i an a mysql statement. here is the issue that i am facing: i have a variable called $date in php.This variable is assigned a valued from a dropdown...
0
by: unixNAB | last post by:
MySQL 5.0.x Hello, I'm trying to work out but the MySQL reference guide to the grant syntax is a little off, it says... so i wanted to do something like grant all on %_log.* to .......;...
7
by: ajos | last post by:
hi friends, i have a problem in mysql query..when i click on submit the values get enterred but i get a ever since i did some changes in my code(earlier i was getting a null pointer...
0
by: DWolff | last post by:
We just converted a database from MySQL 4.1.20 to 5.0 so we can create a view that we may query against. We'd like to use a previous MS Access Query (i.e. MS SQL) to create the View, but are running...
3
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running...
4
by: lyle777 | last post by:
I am having trouble with the nested if statement. can anyone tell me where i went wrong with this code? the syntax error starts by the else statement within the target of the first if statement....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.