Does anyone know how can I write a query to get the desired result to be like tools/lift planning/cranimax from this database
resource_category_id category_name parent_category
1 Tools 0
2 product literature 0
3 Terms and Conitions 0
4 crane library 1
5 geniune 1
6 lift planning 1
8 cranimax 6
For this I wrote a query like this which is not completed, for example this is thing am having in my mind if I choose from cranimax it's having a parent_category 6 so it should make union with that row having resource_category_id and that row having parent_category 1 it should repeat until parent category 0 occurs.
$data['breadcrumbs'] = $this->Manito_model->get_breadcrumbs_details($resource_id);
public function get_breadcrumbs_details($resource_id)
{
$query=$this->db->query("select * from resource_category as m WHERE m.resource_category_id = $resource_id union (select * from resource_category where parent_category != 0) as m2 on m.parent_category = m2.resource_parent_category");
return $query->result();
}
i expect my result to be like this
array (size=3)
0 =>
object(stdClass)[35]
public 'resource_category_id' => string '8' (length=1)
public 'category_name' => string 'cranimax' (length=8)
public 'parent_category' => string '6' (length=1)
public 'created_at' => string '2017-11-04 13:59:39' (length=19)
1 =>
object(stdClass)[35]
public 'resource_category_id' => string '6' (length=1)
public 'category_name' => string 'lift planning' (length=8)
public 'parent_category' => string '1' (length=1)
public 'created_at' => string '2017-11-04 13:59:39' (length=19)
2 =>
object(stdClass)[35]
public 'resource_category_id' => string '1' (length=1)
public 'category_name' => string 'Tools' (length=8)
public 'parent_category' => string '0' (length=1)
public 'created_at' => string '2017-11-04 13:59:39' (length=19)
UNION is used to combine the result from multiple SELECT statements into a single result set (ie making it extremely simple: you need to do two select, but you want only one result, then you make a union)
In you situation you don't need a UNION, but a simple JOIN which "complete" your results with other data:
select m.*, m2.category_name AS parent_name FROM resource_category AS m LEFT JOIN resource_category m2 ON m2.resource_category_id = m.resource_parent_category WHERE m.resource_category_id = $resource_id
Related
Hello friend I have a problem. My id is '28' and I want to show only where has this id, its has first object. How can I make this type of SQL?
WP sql:
$rows = $wpdb->get_results(
"SELECT hotels.id,hotels.hotel,reviews.hotel_id,reviews.id
FROM hotels
INNER JOIN reviews ON hotels.id = reviews.hotel_id" );
var_dump($rows);
var_dump
array (size=4)
0 =>
object(stdClass)[710]
public 'id' => string '28' (length=2)
public 'hotel' => string 'xxxxx' (length=14)
public 'hotel_id' => string '17' (length=2)
1 =>
object(stdClass)[709]
public 'id' => string '29' (length=2)
public 'hotel' => string 'xxxxxx' (length=14)
public 'hotel_id' => string '17' (length=2)
Use WHERE clause
SELECT hotels.hotel,reviews.hotel_id,reviews.id
FROM hotels
INNER JOIN reviews ON hotels.id = reviews.hotel_id
WHERE reviews.id = 28
In Codeigniter I am tryign to join two tables with a one-to-many relation. I want to get one result from my table housetype and all of its values/members from other table housetype_member:
$this->db->select('*');
$this->db->join('housetype_member', 'housetype_member.housetype_id = housetype.PkId', 'left');
$result = $this->db->get_where('housetype', array('PkId' => $id));
return $result->result();
So far I get such result:
array (size=2)
0 =>
object(stdClass)[28]
public 'PkID' => string '4' (length=1)
public 'Name' => string 'Classic' (length=7)
public 'image' => string '1449063250.jpg' (length=14)
1 =>
object(stdClass)[30]
public 'PkID' => string '4' (length=1)
public 'Name' => string 'Classic' (length=7)
public 'image' => string '1449063288.gif' (length=14)
First two object values (PkID, Name) are from the first table and the last one (image) is from the second left table. Everything is good but I get an array with two elements, when I only need one housetype object.
Is there a way to write above code so that my returned object would look like this:
object(stdClass)[28]
public 'PkID' => string '4' (length=1)
public 'Name' => string 'Classic' (length=7)
public 'image' =>
array (size=2)
0 => string '1449063250.jpg' (length=14)
1 => string '1449063288.gif' (length=14)
I need one result from the first table and to it I want to join all of its members from the second table.
Can it be done with Codeigniters active record, can it be done at all?
Add group_by() clause to your query.
$this->db->group_by('housetype.PkId');
In Codeigniter I am tryign to join two tables with one to many relation. I want to get one result from my table housetype and all of its values/members from other table housetype_member:
$this->db->select('*');
$this->db->join('housetype_member', 'housetype_member.housetype_id = housetype.PkId', 'left');
$result = $this->db->get_where('housetype', array('PkId' => $id));
return $result->result();
So far I get such result:
array (size=2)
0 =>
object(stdClass)[28]
public 'PkID' => string '4' (length=1)
public 'Name' => string 'Classic' (length=7)
public 'image' => string '1449063250.jpg' (length=14)
1 =>
object(stdClass)[30]
public 'PkID' => string '4' (length=1)
public 'Name' => string 'Classic' (length=7)
public 'image' => string '1449063288.gif' (length=14)
First two object values (PkID, Name) are from the first table and the last one (image) is from the second left table. Everything is good but I get array with two elements, when I only need one housetype object.
Is there a way to write above code so that my returned object would look like this:
object(stdClass)[28]
public 'PkID' => string '4' (length=1)
public 'Name' => string 'Classic' (length=7)
public 'image' =>
array (size=2)
0 => string '1449063250.jpg' (length=14)
1 => string '1449063288.gif' (length=14)
I need one result from first table and to it I want to join all of its members from the second table.
Can it be done with Codeigniters active record?
As far as your second table has multi records with that primary key, it is better if you don't use joins at all.
You can simply get that with two selects.
$this->db->select('PkID, name');
$this->db->where('PkId', $id);
$houseTypes = $this->db->get('housetype')->result();
foreach($houseTypes as $houseType){
$this->db->select('image');
$this->db->where('housetype_id', $houseType->PKId);
$houseType->image = $this->db->get('housetype_member')->result();
}
return $houseTypes;
I have three tables.
Candidates
Skills
CandidateToSkillMap
Candidate and Skill tables are mapped in CandidateToSkillMap table. In that candidate_id and skill_id are foreign keys.
How can i get following output with them ?
Assume i need to display Computer Skills of candidate_id = 1
Computer Skills -
MS Word (x)
MS Excel (x)
MS Paint ()
It should display distinct list of all available computer skills and there should be a check-box in-front. It will be checked if specific candidate has that skill.
EDIT -
I have query database and loaded data into arrays -
$candidate_profile_computer_skills
this array gives all the computer skills
array (size=3)
0 =>
object(Candidate\Model\CandidateProfileSkill)[317]
public 'id' => string '1' (length=1)
public 'skill_name' => string 'MS Word' (length=7)
public 'candidate_id' => null
public 'candidateprofileskill_id' => null
1 =>
object(Candidate\Model\CandidateProfileSkill)[223]
public 'id' => string '3' (length=1)
public 'skill_name' => string 'MS Excel' (length=8)
public 'candidate_id' => null
public 'candidateprofileskill_id' => null
2 =>
object(Candidate\Model\CandidateProfileSkill)[316]
public 'id' => string '6' (length=1)
public 'skill_name' => string 'MS Paint' (length=8)
public 'candidate_id' => null
public 'candidateprofileskill_id' => null
$candidate_profile_skills_map
this array returns Skills and CandidateToSkillMap with LEFT JOIN for specific candidate (candidate_id = 1)
array (size=5)
0 =>
object(Candidate\Model\CandidateProfileSkill)[321]
public 'id' => string '1' (length=1)
public 'skill_name' => string 'MS Word' (length=7)
public 'candidate_id' => string '1' (length=1)
public 'candidateprofileskill_id' => string '1' (length=1)
1 =>
object(Candidate\Model\CandidateProfileSkill)[322]
public 'id' => string '2' (length=1)
public 'skill_name' => string 'Sinhala Reading' (length=15)
public 'candidate_id' => string '1' (length=1)
public 'candidateprofileskill_id' => string '2' (length=1)
2 =>
object(Candidate\Model\CandidateProfileSkill)[323]
public 'id' => string '3' (length=1)
public 'skill_name' => string 'MS Excel' (length=8)
public 'candidate_id' => string '1' (length=1)
public 'candidateprofileskill_id' => string '3' (length=1)
3 =>
object(Candidate\Model\CandidateProfileSkill)[324]
public 'id' => string '4' (length=1)
public 'skill_name' => string 'English Reading' (length=15)
public 'candidate_id' => string '1' (length=1)
public 'candidateprofileskill_id' => string '4' (length=1)
4 =>
object(Candidate\Model\CandidateProfileSkill)[325]
public 'id' => string '4' (length=1)
public 'skill_name' => string 'English Reading' (length=15)
public 'candidate_id' => string '1' (length=1)
public 'candidateprofileskill_id' => string '4' (length=1)
A join will do:
SELECT
s.skill_name,
CASE WHEN c.candidate_id IS NOT NULL THEN '(X)' ELSE '()' END has_skill
FROM
skills s
LEFT JOIN CandidateToSkillMap c ON s.skill_id = c.skill_id
WHERE
c.candidate_id = 1
If it joins the right side successfully and thus c.candidate_id will be not null, then it means the person has that skill and you mark it else - don't.
You can just use this
Select S.SkillName, count(*) from CandidateToSkillMap CTS, Skills S where S.Id = CTS.CandidateProfileSkillId group by CV.Id having CTS.CandidateId = 1
SELECT * FROM [Skills] s
LEFT JOIN [CandidateToSkillMap] csm ON csm.candidateprofileskill_id = s.id
WHERE csm.candidate_id = 1
This will return all rows from the skills table, and you can do a null-check on the column "candidate_id" to ascertain whether or not the candidate in question has the skill on that row.
Here's some pseudo:
for($row in $result) {
Skill name: $row["skill_name"]
Has skill?: $row["candidate_id"] != null ? "Yes" : "No"
}
you can try this
select S.skill_name from CandidateToSkillMap CSM, Skills s where S.id=CSM.candidatesprofileskill_id and CSM.candidate_id=1
Try something like
SELECT s.skill_name, (cm.Candidate_id IS NOT NULL) AS HasSkill
FROM Skills AS s
LEFT OUTER JOIN candidateToSkillMap AS cm
ON s.id = cm.candidateprofileskill_id
WHERE s.skill_type = 'Computer Literacy'
AND (Candidate_id = 1 OR Candidate_id IS NULL)
The above query uses LEFT OUTER JOIN to join the skills and candidatestoskillmap include all the rows from skills tables. This will ensure that any rows not matched in the candidatestoskillmap table are included as null values.
The select section then looks for these values and assigns a true/false for it.
The Where section filters for the skill type and the candidate's id.
I have a var dump of my sql query which return the following
I wanna to count in the array below that how many rows of myID = 5 are there. How would I do that. I am using php. Thanks in advance
array
0 =>
object(stdClass)[17]
public 'myID' => string '5' (length=1)
public 'data' => string '123' (length=3)
1 =>
object(stdClass)[18]
public 'myID' => string '5' (length=1)
public 'data' => string '123' (length=3)
2 =>
object(stdClass)[19]
public 'relativeTypeID' => string '2' (length=1)
public 'data' => string '256' (length=3)
3 =>
object(stdClass)[20]
public 'myID' => string '4' (length=1)
public 'data' => string '786' (length=3)
object(stdClass)[21]
public 'myID' => string '4' (length=1)
public 'data' => string '786' (length=3)
Do you always have the same value of data for the same myID? In other words, is data functionally dependant on myID?
If so, you can get the database to do this for you:
SELECT myID, data, COUNT(*) AS cnt
FROM (your query here)
GROUP BY myID, data
This would give you results like the following:
myID data cnt
'5' '123' 3
'2' '256' 1
'4' '786' 2
Or, you can use a foreach statement, like:
$count = 0;
foreach($arr as $item)
{
// Given that your item is an stdClass Object you access its property with "->"
// if an array $item["myID"] instead
if ( $item->myID == '4' )
{
$count ++;
}
}