Pages

Showing posts with label question. Show all posts
Showing posts with label question. Show all posts

Tuesday, November 18, 2014

Mysql Puzzle(Left Join)-Take out those department who have no employees assigned to it

EMP
EMPNO   ENAME    DEPTNO

DEPT
DEPTNO   DNAME
 
 
1st Way using Subquery
 
SELECT D.DNAME
FROM DEPT D
WHERE 
NOT EXISTS (SELECT * FROM EMP E WHERE D.DEPTNO = E.DEPTNO) 

2nd way using Left Join
 
SELECT D.DNAME
FROM DEPT D
LEFT JOIN EMP E ON D.DEPTNO = E.DEPTNO
WHERE E.DEPTNO IS NULL 

Thursday, September 12, 2013

Javascript-Typecasting-Question

What will this print

<script type="text/javascript">
alert('1'+2+4);
</script>


Ans : 124(since js typcasts everthing to string if a string is encountered)


When dealing with two numbers, the + operator adds these. When dealing with two strings, it concatenates.

3 + 5;              // 8
"hello " + "world"; // "hello world"
"3" + "5";          // "35"
 
 
When one operand is a string and the other is not, the other is converted to a string and the result is concatenated. 

"the answer is " + 42; // "the answer is 42"
"this is " + true;     // "this is true"
var a;
"a is " + a;           // "a is undefined"
 
In any other case (except for Dates) the operands are converted to numbers and the results are added.
 
1 + true;     // -> 1 + 1 -> 2
null + false; // -> 0 + 0 -> 0
 
 

Sunday, January 27, 2013

Scaffolding in Cakephp

Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects.

To add scaffolding to your application, in the controller, add the $scaffold variable.

class CategoriesController extends AppController {
    var $scaffold;
}

Scaffolding a way to automatically look at what a table has and its very basic front end to check its CRUD Operations

Scaffolding automatically reads relations defined in a model

Foe example if your table has a field of tinyint attribute with length 1 then in that case cakephp will treat the same as a checkbox.

When we use a "hasMany" relation in a Model the form will show a dropdown

When we use a "has and belongs to Many" relation in a Model the form will show a multiselect list box

When we use a "belongsTo" relation in a Modelthe form will show a dropdown

The Scaffolding Structure(Form in view) shows the relation with an "Alias Name" given for the relation defined in a model.

Foe example if we have define a Table "Persons" and another table as "Horses" then we define the relation in a "Horses" model as

var $belongsTo=array('Owner'=>array('classname'=>'Person','foriegnKey'=>'owner_id'))

then Scaffolding for the  model will show the field name for owner_id as Owner.

also for a case like

var $hasAndBelongsToMany=array('Riders'=>array('className'=>'Person'));

and in Persons model we define

var $hasAndBelongsToMany=array('Horse');

we will find a multiselect list box with name "Riders" with automatically picked up names from Persons table.


Friday, December 7, 2012

Interview Question on relations in cakephp

Recently I was asked on different types of relations in between models in a cakephp framework

Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to many ingredients.

Then I was given 4 tables as follows

Employee:
Eid,fname,lname

Department:
Deptid,deptname

Salary: 
Sid,salary

EmpSalDeptrelation:
eid,sid,deptid

To these I was told to determine how relations will be represented in EmpSalDeptrelation table so that if I fetch 1 employee I get his salary department along

Answer is to the EmpSalDeptrelation model add the relationships as

eid belongsTo Eid of employee,sid belongs to Sid of salary,deptid belongs to Deptid of Department

and then in the respective controller which uses($uses) this model
add
$this->empsaldeptrelation->find();
set the conditions where eid equaks to concerned employee id;