Inner joins SQL in WP environment with where id =id - php

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

Related

Query with union condition

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

SQL JOIN returns query with wrong ids for each row

In my database I have a table named posts and a table named topics.
Each post has a parent topic.
In this case posts.parent_topic is a foreign key to the associated topics.id
I am attemping to do a JOIN so that my query will return the correct topics.topic_title from the topics table. I have been able to successfully do this, however I am noticing that I am no longer getting the correct posts.id for anything that is returned.
My query:
$posts = $db->query("
SELECT
*
FROM
posts
JOIN topics ON
posts.post_parent_topic = topics.id
ORDER BY
posts.created_on DESC
")->fetchALL(PDO::FETCH_ASSOC);
After doing a var_dump on the $posts variable I am seeing these results:
array (size=2)
0 =>
array (size=12)
'id' => string '1' (length=1) // this id is CORRECT for the post
'post_parent_topic' => string '1' (length=1)
'created_on' => string '2015-11-03 09:30:40' (length=19)
'post_title' => string 'Development Standards' (length=21)
'post_content' => string 'These are the development specifc standards.' (length=44)
'topic_title' => string 'Code Standards' (length=14)
'topic_content' => string 'These are the company programming standards.' (length=44)
'topic_parent_organization' => string '1' (length=1)
1 =>
array (size=12)
'id' => string '1' (length=1) // this id is INCORRECT for the post, it should be an id of 2 (as it appears in the database)
'post_parent_topic' => string '1' (length=1)
'created_on' => string '2015-11-03 09:30:40' (length=19)
'post_title' => string 'Design Standards' (length=16)
'post_content' => string 'These are the design specific standards.' (length=40)
'topic_title' => string 'Code Standards' (length=14)
'topic_content' => string 'These are the company programming standards.' (length=44)
'topic_parent_organization' => string '1' (length=1)
Why would each post return the same id? I need the id of the post as it is in my database.
If you have id column in both posts and topics tables, you may need to give them aliases to make it unambigious:
Try changing the query to the following:
SELECT p.*, t.id AS TopicId, t.topic_title, t.topic_content, t.topic_parent_organization
FROM posts AS p
JOIN topics AS t
ON posps.post_parent_topic = t.id
ORDER BY p.created_on DESC

Model uses MySQL view and some fields are not at the same level/hiearchy

I've configured a model to use a view. When querying this view in my controller, the recordset returned has two fields that are not at the same level/hiearchy than the rest.
In this case, see ast (asset_id and asset_configuration_id).
Querying the view manually in dbForge, I get a flat recordset. I cannot figure out why this is happening.
Model:
<?php
App::uses('AppModel', 'Model');
class AssetConfigurationDeploymentStatus extends AppModel {
public $useTable = "asset_configuration_deployment_statuses";
}
?>
Query for view asset_configuration_deployment_statuses:
SELECT `ac`.`id` AS `asset_configuration_id`
, `ast`.`id` AS `asset_id`
, `ac`.`domain_id` AS `domain_id`
, `ac`.`server_id` AS `server_id`
, `e`.`acronym` AS `environment`
, `e`.`id` AS `environment_id`
, `d`.`name` AS `domain`
, `lds`.`deployed_by` AS `last_deployed_by`
, `lds`.`deployed_date` AS `last_deployed_date`
, `lds`.`revision` AS `last_deployed_revision`
, `lds`.`is_failed` AS `last_deployed_is_failed`
, `lss`.`deployed_by` AS `last_successful_deployed_by`
, `lss`.`deployed_date` AS `last_successful_deployed_date`
, `lss`.`revision` AS `last_successful_deployed_revision`
, `lss`.`is_failed` AS `last_successful_deployed_is_failed`
, `eds`.`count_failed_deployments` AS `count_failed_deployments`
, `er`.`last_successful_environment_revision` AS `last_successful_environment_revision`
, `ast`.`asset_group_id` AS `asset_group_id`
FROM
(((((((((`assets` `ast`
JOIN `asset_configurations` `ac`
ON ((`ast`.`id` = `ac`.`asset_id`)))
JOIN `last_deployment_statuses` `lds`
ON ((`ac`.`id` = `lds`.`asset_configuration_id`)))
JOIN `last_successful_deployment_statuses` `lss`
ON ((`ac`.`id` = `lss`.`asset_configuration_id`)))
JOIN `last_environment_deployment_statuses` `eds`
ON ((`ac`.`id` = `eds`.`asset_configuration_id`)))
JOIN `domains` `d`
ON ((`ac`.`domain_id` = `d`.`id`)))
JOIN `servers` `s`
ON ((`ac`.`server_id` = `s`.`id`)))
JOIN `environments_servers` `es`
ON ((`s`.`id` = `es`.`server_id`)))
JOIN `environments` `e`
ON ((`es`.`environment_id` = `e`.`id`)))
JOIN `last_successful_environment_deployment_revision` `er`
ON (((`ac`.`asset_id` = `er`.`asset_id`) AND (`e`.`id` = `er`.`environment_id`))))
Controller code:
...
$this->loadModel("AssetConfigurationDeploymentStatus");
$all_deploy_items = $this->AssetConfigurationDeploymentStatus->find('all', array(
'order'=>array('AssetConfigurationDeploymentStatus.environment_id','AssetConfigurationDeploymentStatus.domain'),
'conditions' => array('AssetConfigurationDeploymentStatus.asset_id' => $id)
)
);
foreach($all_deploy_items as $conf){
var_dump($conf);
exit;
...
}
...
Output of dump:
array (size=2)
'AssetConfigurationDeploymentStatus' =>
array (size=16)
'asset_configuration_id' => string '172' (length=3)
'domain_id' => string '21' (length=2)
'server_id' => string '10' (length=2)
'environment' => string 'DEV' (length=3)
'environment_id' => string '4' (length=1)
'domain' => string 'my.domain.here' (length=28)
'last_deployed_by' => string 'user' (length=7)
'last_deployed_date' => string '2014-06-23 12:05:24' (length=19)
'last_deployed_revision' => string '644' (length=3)
'last_deployed_is_failed' => boolean false
'last_successful_deployed_by' => string 'user' (length=7)
'last_successful_deployed_date' => string '2014-06-23 12:05:24' (length=19)
'last_successful_deployed_revision' => string '644' (length=3)
'last_successful_deployed_is_failed' => boolean false
'count_failed_deployments' => string '0' (length=1)
'last_successful_environment_revision' => string '1930' (length=4)
'ast' =>
array (size=2)
'asset_id' => string '47' (length=2)
'asset_group_id' => string '28' (length=2)

Join two tables and display distinct list of column

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.

php array count

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 ++;
}
}

Categories