perform inner join on same table in mysql

During application development, i was stuck in one mysql – query problem.  i don’t know whether this post title gives meaning or not. but I had used this title to search on google for my problem’s solution.  so i kept same here.

let me describe problem

+-------------------------+---------+
| switch                  | port_no |
+-------------------------+---------+
| 00:00:00:00:00:00:00:02 | 3       |
| 00:00:00:00:00:00:00:01 | 2       |
| 00:00:00:00:00:00:00:01 | 1       |
| 00:00:00:00:00:00:00:02 | 1       |
| 00:00:00:00:00:00:00:04 | 2       |

so i have such type of data in sla table.  and i want bellow output

Expected output

+-------------------------+---------+
| switch                  | port_no |
+-------------------------+---------+
| 00:00:00:00:00:00:00:02 | 3,1     |
| 00:00:00:00:00:00:00:01 | 2,1     |
| 00:00:00:00:00:00:00:04 | 2       |

so here is magic function which saved my life 🙂

Function :

Look at GROUP_CONCAT()

Query which return expected output

select switch,GROUP_CONCAT(port_no) from sla group by switch;

i know lots of newbie developers got such problem so i’m sharing here..
if anyone have best title for it .. plz suggest it in comment.

🙂

REST Client Example in Ruby

REST Client – simple DSL for accessing HTTP and REST resources

A simple HTTP and REST client for Ruby, inspired by the Sinatra’s microframework style of specifying actions: get, put, post, delete.

I found this Ruby-Gem very useful while building App. so i will describe here how to use REST Client in Ruby

Simple example for REST Client POST Request.

require 'rest_client'


res=RestClient.post 'http://localhost:8080/wm/test/json', params.to_json, :content_type => :json, :accept => :json
result = JSON.parse(res)
return result.to_json

above example send post request to my local server, which takes few parameters in form of json and

result.to_json

return output in form of JSON.

likewise, you can use any method GET,PUT,DELETE.

Hope you’ll like dis 🙂

Posting JSON data to sinatra with JQuery

I guess this post is mainly useful when you want to build Restful API with Sinatra.   Let’s say you want to pass JSON data to Sinatra backend using JQuery. so you may got very first question is How to parse JSON POST Data in Sinatra rest api ?  am i right ?

so here is full example which demonstrate how to send JSON data to Sinatra REST API.

JQuery Code:

$.ajax({
url: '/test_api',
type: 'POST',
dataType: 'json',
contentType: "application/json",
data:JSON.stringify(data),
success: function(data){
}
})

let’s say data (json variable) contain complex data like

{
"topo": [
    {
      "dpid": "00:00:00:00:00:00:00:03",
      "ports": [
                 3,
                 2
                ]
    }
   ],
"app": "vm_migration"
}

Ruby Sinatra Rest Function

post '/test_api', :provides => :json do
  begin
     # bellow line get post json data
     params = JSON.parse(request.env["rack.input"].read) 
     # you can access key value like bellow
     app = params["app"]
     params["topo"].each do |topo|
            dpid = topo["dpid"]
            # getting each value of key using each
            topo["ports"].each do |port| 
                #Process Data
            end            
        end
  rescue Exception => e
    return e.message
  end
end

Hope it is useful..  plz do comment if it is useful for you..
🙂

any question related to post, feel free to ask.