mardi 1 septembre 2015

How to improve an IF statement in MySQL?

Consider the mysql

SELECT If(
            (SELECT `price` FROM `prices` AS `B` WHERE `A`.`Item_ID` = `B`.`Item_ID`)<5,
            5,
            (SELECT `price` FROM `prices` AS `B` WHERE `A`.`Item_ID` = `B`.`Item_ID`)
        ) AS `Item_Price' 
FROM `items` AS `A`

where it used to find the price from prices table for each item from the items table, and If the price is less than 5, then it is to 5. The problem with this is that if the IF statement comes false, i.e. the price greater or equal to 5, then it runs another search for the same price which may slow down the page. Is there a way to improve this?

Combining if-then statements

I'm trying to convert individual integers to from their ASCII values to 0s and 1s. The relevant code looks something like this:

    int num1 = bin.charAt(0);
    int num2 = bin.charAt(1);
    int num3 = bin.charAt(2);
    int num4 = bin.charAt(3);

    if (num1 == 49) 
    {
        num1 = 1;
    }
    else
    {
        num1 = 0;
    }

    if (num2 == 49) 
    {num2 = 1;}
    else
    {
        num2 = 0;
    }

Is there a way to combine if/else statements so that I don't have 30 lines of code of the same principle?

I tried if (num1 == 49 || num2 ==49) but I'm not sure how to only assign the value that is equal to 49 to 0.

How to get data between two timestamps

I have one timestamp like 2015-08-31-21.43.47.986782 and I need to get all the data captured between this timestamp and the current system timestamp. How can i do it? Do i need to go for any kind of condition? If yes, please elaborate

Typeahead multiple remote datasets - unable to select via If statement

I am using v0.10.5 of the Twitter Typeahead library and have used the remote dataset example to connect to two datasets, but I am having problems when trying to run code based upon which dataset the user has searched on.

I have tried using an If statement but this doesn't seem to work (nothing appears in the console log).

Is there anything obvious I am doing wrong? Is there a different way to specify a particular dataset in the on:selected event?

var addresses = new Bloodhound({
  name: "addresses",
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('ADDRESS'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'php/search_addresses.php?query=%QUERY',
    wildcard: '%QUERY',
    ajax: {
      beforeSend: function (jqXhr, settings) {
        $("#searchicon").removeClass("fa-search").addClass("fa-refresh fa-spin");
      },
      complete: function (jqXHR, status) {
        $('#searchicon').removeClass("fa-refresh fa-spin").addClass("fa-search");
      }
    }
  },
  limit: 50
});

var planning = new Bloodhound({
  name: "planning",
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('APPLICATION_NUMBER'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'php/search_planning.php?query=%QUERY',
    wildcard: '%QUERY',
    ajax: {
      beforeSend: function (jqXhr, settings) {
        $("#searchicon").removeClass("fa-search").addClass("fa-refresh fa-spin");
      },
      complete: function (jqXHR, status) {
        $('#searchicon').removeClass("fa-refresh fa-spin").addClass("fa-search");
      }
    }
  },
  limit: 50
});

addresses.initialize();
planning.initialize();

$("#searchbox").typeahead({
  minLength: 5,
  hint: false
},
{
  name: 'addresses',
  displayKey: 'ADDRESS',
  source: addresses.ttAdapter(),
  templates: {
    header: "<h4 class='typeahead-header'>Addresses</h4>"
  }
},
{
  name: 'planning',
  displayKey: 'APPLICATION_NUMBER',
  source: planning.ttAdapter(),
  templates: {
    header: "<h4 class='typeahead-header'>Planning Applications</h4>"
  }
}).on("typeahead:selected", function (obj, datum) {
  if (datum.name === "addresses") {
    console.log("address selected"); 
  }
  if (datum.name === "planning") {
    console.log("planning selected");
  }
});

How to change the global variable value inside the loop or conditions in .bat?

Here's my batch command.

@ECHO OFF
setlocal enabledelayedexpansion
SET ONE=1
for /r C:\Users\office\Desktop\dump %%i in (*.jpg) do(
 IF %ONE%==1 (
   ECHO value is 1
   SET ONE=2
) ELSE IF %ONE%==2 (
   ECHO value is 2
) ELSE (
    ECHO no value
)
)

I try to set the value to ONE again in condition, but it's not working,how to set the global variable value in batch command.

object motions bound by to points

I'm want to animate the movement of a piston of a cylinder. i set the max and min points of the motion, but how can i leave to max/min point when the piston reach the end points?

I use a if function containing the arguments. it works fine but i'm not sure how to define the else argument.

void Update ()
{
    if (transform.position.y > 0) {

        if (Input.GetKey (KeyCode.UpArrow))
            transform.Translate (Vector3.up * moveSpeed * Time.deltaTime);

        if (Input.GetKey (KeyCode.DownArrow))
            transform.Translate (-Vector3.up * moveSpeed * Time.deltaTime);

    } else if (transform.position.y < 3) {

        if (Input.GetKey (KeyCode.UpArrow))
            transform.Translate (Vector3.up * moveSpeed * Time.deltaTime);

        if (Input.GetKey (KeyCode.DownArrow))
            transform.Translate (-Vector3.up * moveSpeed * Time.deltaTime);
    } else {

        if (Input.GetKey (KeyCode.UpArrow))
            transform.Translate (Vector3.up*0);

        if (Input.GetKey (KeyCode.DownArrow))
            transform.Translate (Vector3.down*0);
    }
}

it works fine when i animate one border at the time, but when i combine them, the piston keeps moving outside the borders also.

my questions are:

  1. How do i best define the else condition(outside the borders)
  2. and how do i leave the endpoints?

Does Shorted if evaluate statement twice?

I have the following code:

int num = (map.get(result.getNumber()) != null ? map.get(result.getNumber()) : 0);

My question is:

Do map.get performed twice (one for the evaluation and one for the placement)?

My goal is to reduce the get statement into single call.

Note: result.getNumber is nullable.