I am trying to count the number of data in the database according to the category id and I have written the following lines of code:
public function getPodcastByCategoryId($catId){
$args = array(
'fields' => array(
'podcast.id',
'podcast.title',
'podcast.description',
'podcast.duration',
'podcast.audio',
'podcast.image',
'podcast.category',
'podcast.added_date',
'categories.title AS category_title',
'(SELECT users.full_name FROM users WHERE id = podcast.added_by) as author',
'(SELECT COUNT(category) FROM podcast WHERE category = podcast.category) as episodes'
),
'join' => "LEFT JOIN categories on podcast.category = categories.id",
'where' => array(
'category' => $catId
),
);
return $this->select($args);
}
However, the function getPodcastByCategoryId($catId) is giving episodes = 3 which is the count of data present in table podcast.
Data in database:
My expected episodes would be 2 in category id 2 and 1 in category id 1.
for episode you should use COUNT(*)
$args = array(
'fields' => array(
'podcast.id',
'podcast.title',
'podcast.description',
'podcast.duration',
'podcast.audio',
'podcast.image',
'podcast.category',
'podcast.added_date',
'categories.title AS category_title',
'(SELECT users.full_name FROM users WHERE id = podcast.added_by) as author',
'(SELECT COUNT(*) FROM podcast WHERE categories.category = podcast.category) as episodes'
),
'join' => "LEFT JOIN categories on podcast.category = categories.id",
'where' => array(
'category' => $catId
),
);
Related
I make a custom search in WordPress. There are three fields for searching.
Age
Location
Post Title
Post Title is searched from wp_posts > post_title field and location and age are custom meta fields y_age, y_activity_locations. So there is 6 scenarios of searching.
If user enter:
Enter Title or
Select Location or
Select Age or
Enter Title and Select Location or
Enter Title and Select Age or
Select Location and Select Age
My first 5 (Five) scenarios are working perfectly but my 6th scenario is not working because it comes from same table (wp_postmeta) and same column but two different values.
So I tried this query:
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
And y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%'
The query is not working because I think sever confused two values from same column.
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
Or y_meta.meta_key = 'y_age'
Or y_meta.meta_value Like '%3 to 5%'
This query works and it gives me only age or location data but i want search both values at a same time.
I also tried sub query (Never tried before)
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
And (Select yy_meta.* From wp_postmeta As yy_meta Where yy_meta.meta_key = 'y_age'
And yy_meta.meta_value Like '%3 to 5%')
But it gives me this error
1241 - Operand should contain 1 column(s)
So please guide me how can I get two values from same column.
Please give me only query suggestions not any other solutions.
Please try following query :
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And (
(y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%')
Or (y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%')
)
You try, wordpress query
<?php
$argsCat = array(
'posts_per_page' => -1,
'post_type' => 'download',
'meta_key' => 'y_activity_locations',
'meta_value' => '%Armidale%',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '3',
'compare' => '>='
),
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '5',
'compare' => '<='
)
),
);
$normal_array = get_posts($argsCat);
?>
I'm trying to return a blog_post,blog_title from a MYSQL databse but I also need the user and his/her's data (in seperate array's)
The output should be like
array(
'blog1' => array(
'title' => 'this is a title',
'post' => 'This is the blogs content',
'user' => array(
'username' => 'Name',
'lastname' > 'Lastname'
)
),
'blog2' => array(
'title' => 'this is a title',
'post' => 'This is the blogs content',
'user' => array(
'username' => 'Name',
'lastname' > 'Lastname'
)
)
)
Using mysql there is no way of doing this in one query because it will mix up the two tables.
I have tried using left_join,right_join and selecting from multiple tables like
SELECT * FROM blogs a, users b WHERE b.id = a.id
Then I have tried using a foreach and a while loop.
$posts = array();
$x = 0;
while(++$x < 20){
$post = DB::query('SELECT * FROM posts WHERE id = '.$id.' ');
$post['user'] = DB::query('SELECT * FROM users WHERE id = '.$post['user_id'].' ');
$posts[] = $post;
}
But this will always return the same post/user
If you need a posts for a particular user it is possible to do like this (assumed you have a relation in posts table to users table by foreign key fk_user for each post):
$user_id = 1;
$posts = DB::query(
'SELECT posts.*,
users.username,
users.lastname
FROM posts
JOIN users ON users.id = posts.fk_user
WHERE user.id = '.$user_id
);
In case you need just a list of posts with related user info you can act as follows:
$posts = DB::query(
'SELECT posts.*, users.username, users.lastname
FROM posts
JOIN users ON users.id = posts.fk_user
LIMIT 100'
); // use your limit here
After you have a $posts array filled you can iterate and rearrange (if you need) or just use it as is:
foreach( $posts as $post ) {
// print post info here or rearrange array
$post['user'] = [
'username' => $post['username'],
'lastname' => $post['lastname'],
];
}
How do I translate below to cakePHP code? The code below comes from a solution here MySQL order by before group by.
Getting the author's latest post using group by.
SELECT p1.*
FROM wp_posts p1
INNER JOIN
(
SELECT max(post_date) MaxPostDate, post_author
FROM wp_posts
WHERE post_status='publish'
AND post_type='post'
GROUP BY post_author
) p2
ON p1.post_author = p2.post_author
AND p1.post_date = p2.MaxPostDate
WHERE p1.post_status='publish'
AND p1.post_type='post'
order by p1.post_date desc
Below is still similar situation:
SELECT t1.* FROM payment_status t1
JOIN (SELECT payment_id, MAX(created) max_created
FROM payment_status
GROUP BY payment_id
) t2
ON t1.payment_id = t2.payment_id AND t1.created = t2.max_created;
I need some cakePHP translation for either the two mySQL statements.
------------------------------------------------------------------------------------
I did something like the code below but it gives me error
Error: SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'SELECT max(dateEncoded) maxDate, findings FROM maintain GROUP BY computer_id Office.main_office LIKE' is too long
How do I fix it?
$this->Computer->unbindModel(array(
'belongsTo' => array('Office'),
'hasMany' => array('Brand','Maintain')
));
$model_view = $this->Computer->bindModel(array(
'hasOne' => array(
'Office' => array(
'foreignKey' => false,
'conditions' => array('Office.id = Computer.office_id')
),
'Maintain' => array(
'foreignKey' => false,
'conditions' => array('Computer.id = Maintain.computer_id'),
)
)
)
);
$main_office = trim($this->request->data['Office']['office_id']);
$joins = array(
array(
'table' => "SELECT max(dateEncoded) maxDate, findings FROM maintain GROUP BY computer_id",
'alias' => 'P2',
'type' => 'INNER',
'conditions' => array('Maintain.findings = p2.findings','Maintain.dateEncoded = p2.maxDate')
)
);
$conditions=array("Office.main_office LIKE"=>"%$main_office%");
$result = $this->Computer->find('all',array(
$model_view,
'joins'=>$joins,
'conditions'=>$conditions,
'order' => array('Office.description'),
'group' => 'Computer.id'
));
This can be written something like this :-
$joins = array(
array(
'table' => 'SELECT max(post_date) MaxPostDate, post_author FROM wp_posts WHERE post_status='publish' AND post_type='post'GROUP BY post_author',
'alias' => 'P2',
'type' => 'INNER',
'conditions' => array('WpPost.post_author = p2.post_author','WpPost.post_date = p2.MaxPostDate')
)
);
$conditions=array("WpPost.post_status='publish'","WpPost.post_type='post'");
$this->WpPost->find('all',array('fields'=>array('WpPost.*'),'joins'=>$joins,'conditions'=>$conditions);
Contributors have songs and songs have contributors. I want to be able to sort by the number of songs that a contributor has.
In my Controller:
public $paginate = array(
'fields' => array(
'Contributor.id',
'Contributor.name',
'COUNT(DISTINCT ContributorsSong.song_id) AS Contributor__TotalSongs',
),
'joins' => array(
array(
'alias' => 'ContributorsSong',
'table' => 'contributors_songs',
'type' => 'LEFT',
'conditions' => 'ContributorsSong.contributor_id = Contributor.id'
),
array(
'alias' => 'Song',
'table' => 'songs',
'type' => 'LEFT',
'conditions' => 'ContributorsSong.song_id = Song.id'
)
),
'group' => array('ContributorsSong.contributor_id')
);
And in my index method.
$this->Contributor->recursive = 0;
$this->Paginator->settings = $this->paginate;
$this->Contributor->virtualFields['TotalSongs'] = 0;
$items = $this->paginate();
echo '<pre>';print_r($items);echo '</pre>';
I'm trying to sort by the number of songs by using a virtual field, so when I go to
localhost/site/contributors/index/sort:TotalSongs/
I get this error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause'
SQL Query:
SELECT Contributor.id,
Contributor.name,
COUNT(DISTINCT ContributorsSong.song_id) AS Contributor__TotalSongs
FROM db_songs2.contributors AS Contributor LEFT JOIN
db_songs2.contributors_songs AS ContributorsSong ON
(ContributorsSong.contributor_id = Contributor.id) LEFT JOIN
db_songs2.songs AS Song ON (ContributorsSong.song_id =
Song.id) WHERE 1 = 1 GROUP BY ContributorsSong.contributor_id
ORDER BY (0) desc LIMIT 18
I thought that TotalSongs would get turned into Contributor__TotalSongs in the query but it gets turned into 0. What is going on here? Thanks.
I replaced:
$this->Contributor->virtualFields['TotalSongs'] = 0;
with:
$this->Contributor->virtualFields['TotalSongs'] = 'COUNT(DISTINCT ContributorsSong.song_id)';
And deleted:
'COUNT(DISTINCT ContributorsSong.song_id) AS Contributor__TotalSongs',
And it works for ordering now, but I can't use it in the conditions. I think this is part of the Limitations of virtualFields? I'm trying to select only tables where TotalSongs > 0, but I think I can get this another way, by changing the join from LEFT to INNER.
I'm trying to join a Users table to my curent hasMany Through table which has Interest and User model id's.
Below is the find query with options:
$params = array(
'fields' => array('*', 'COUNT(DISTINCT(InterestsUser.interest_id)) as interest_count'),
'limit' => 15,
'recursive' => -1,
'offset' => $offset,
'conditions' => array('InterestsUser.interest_id' => $conditions),
'group' => array('InterestsUser.user_id'),
'order' => array('interest_count DESC', 'InterestsUser.user_id ASC', 'InterestsUser.interest_id ASC'),
'joins' => array(
array('table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'User.id' => 'InterestsUser.user_id',
)
)
)
);
$results = $this->InterestsUser->find('all', $params);
This returns InterestsUser table fine but does not return any values for Users table. It only returns field names.
What could be wrong?
UPDATE:
OK, above is generating below SQL which I got from Cake's datasources sql log:
SELECT *, COUNT(DISTINCT(InterestsUser.interest_id)) as interest_count
FROM `interests_users` AS `InterestsUser`
LEFT JOIN users AS `User` ON (`User`.`id` = 'InterestsUser.user_id')
WHERE `InterestsUser`.`interest_id` IN (3, 2, 1)
GROUP BY `InterestsUser`.`user_id`
ORDER BY `interest_count` DESC, `InterestsUser`.`user_id` ASC, `InterestsUser`.`interest_id` ASC
LIMIT 15
Why is users table values returning NULL only for all fields?
UPDATE:
OK I tried below but this is working fine...What am I missing here!!??
SELECT * , COUNT( DISTINCT (
interests_users.interest_id
) ) AS interest_count
FROM interests_users
LEFT JOIN users ON ( users.id = interests_users.user_id )
WHERE interests_users.interest_id
IN ( 1, 2, 3 )
GROUP BY interests_users.user_id
ORDER BY interest_count DESC
LIMIT 15
The array syntax for join conditions should be like the following
array('User.id = InterestsUser.user_id')
as opposed to array('User.id' => 'InterestsUser.user_id'). For more, see http://book.cakephp.org/view/1047/Joining-tables.