My query looks like this:
SELECT * FROM `tasks` as t
LEFT JOIN `task2status` as t2s ON t2s.id = (SELECT t2s1.id FROM task2status as t2s1 WHERE t2s1.task_id = t.id ORDER BY t2s1.id DESC LIMIT 1)
In search model:
$query = Tasks::find()->alias('t');
$subQuery = Task2status::find()->alias('t2s')->select('t2s.id')->where(['t2s.task_id' => 'tasks.id'])->orderBy('t2s.id desc')->limit(1)->offset(0);
$query->InnerJoin('task2status ts', ['ts.id' => $subQuery]);
As a result, I get the following
SELECT COUNT(*) FROM `tasks` `t` INNER JOIN `task2status` `ts` ON `ts`.`id` IN (SELECT `t2s`.`id` FROM `task2status` `t2s` WHERE `t2s`.`task_id`='tasks.id' ORDER BY `t2s`.`id` DESC LIMIT 1)
The 'limit' can not be used in 'in'. Accordingly, an error occurs
And I need to get '=' instead of 'in'. How to get this? what am I doing wrong?
Thank you in advance, and sorry for the bad English
If the limit is 1 there is no need for 'in'. Please change limit to 2 and check
Related
Below is my 1st query where I use to get post data and creator data who creates it in post.date_created order from post and account_data_base tables.
SELECT post.*, account_data_base.* FROM post
JOIN account_data_base ON post.creator_id =
account_data_base.user_uid ORDER BY post.date_created DESC
and this is my 2nd query in php after running 1st query which I use to check if the post creator likes his/her post or not.
$stmt = $conn->prepare("SELECT EXISTS(SELECT * FROM like
WHERE post_id = ? and userLike_Id = ?)");
$stmt->bind_param("ss", $postId, $account_data_base_id);
as you can see $postId and $account_data_base_id are values that I get from my 1st query. I wonder is there a way that I can combine 2 queries at once where I can get post data, creator data as well as my 2nd query to check like or not?
Update: this is what I tried but the like check doesn't show accurate info:
SELECT post.*, account_data_base.*,
EXISTS(SELECt * FROM like WHERE like.post_id = post.id and like.userLike_Id = account_data_base.user_uid) FROM post
JOIN account_data_base ON post.creator_id =
account_data_base.user_uid ORDER BY post.date_created DESC
You can use your EXISTS query as subquery in the SELECT clause:
SELECT post.*, a.*, EXISTS (
SELECT * FROM `like` l
WHERE l.post_id = p.id
and l.userLike_Id = a.id
) as likeExists
FROM post p
JOIN account_data_base a ON p.creator_id = a.user_uid
ORDER BY p.date_created DESC
This will add the field likeExists which is either 1 or 0. And you don't need to run the query once per post.
I want to get list of posts sorted by number of comments, I've successfully ran following query but it gives repetitive values i.e posts repeat, I want unique of them sorted by number of comments, when I put DISTINCT() around my whole query, an error appears:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNIQUE(post.pname, post.pid FROM post, COMMENT WHERE post.pid = comment.pid ORD' at line 1
Query without DISTINCT() (Works but of course doesn't give unique values)
SELECT post.pname, post.pid
FROM post,
COMMENT WHERE post.pid = comment.pid
ORDER BY (
SELECT COUNT( * )
FROM COMMENT WHERE comment.pid = post.pid
GROUP BY post.pname
)
Query with DISTINCT() (doesn't work)
SELECT DISTINCT(post.pname, post.pid
FROM post,
COMMENT WHERE post.pid = comment.pid
ORDER BY (
SELECT COUNT( * )
FROM COMMENT WHERE comment.pid = post.pid
GROUP BY post.pname
))
DISTINCT should be used thus:
SELECT DISTINCT a,b,c FROM t;
without a GROUP BY. It will find all the (a,b,c) in the table, then de-dup them.
This is broken:
SELECT id, a, b FROM t GROUP BY id;
That is because it will find all the distinct values of id, but supply random values of a and b to go with each.
To find out how many of each foo there are, this pattern works nicely:
SELECT foo, COUNT(*) FROM t GROUP BY foo;
Don't use () after DISTINCT.
Since I don't understand what you are looking for, I may or may not have provided you enough info to fix your query. If I have failed, please provide some sample data and the desired output; sometimes reverse engineering is the easiest way to figure it out.
SELECT distinct(post.pname) FROM post,COMMENT WHERE post.pid = comment.pid ORDER BY (SELECT COUNT( * ) FROM COMMENT WHERE comment.pid = post.pid GROUP BY post.pname) DESC
SELECT post.pname, post.pid
FROM post
Inner Join ( Select
comment.pid
, COUNT(*) As Cant
From COMMENT
Group By
comment.pid
) As x
On post.pid = x.pid
ORDER BY x.Cant
SELECT p.*
FROM post p JOIN comment c ON c.postId = p.id
GROUP BY p.id
ORDER BY COUNT(*)
i have a table with date(actually they are string time in this format: 2004-Mar). I want to rank the record based on this date, so i have the following query:
SELECT *,STR_TO_DATE(detail,'%Y-%b')
FROM table2 JOIN user_table
ON table2.user_id = user_table.id
ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC
WHERE table2_col = 11;
But this query doesn't work, it asks me to check syntax err near 'WHERE table2_col = 11' at line 5
If i delete ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC , then everything works fine. so i think the error comes from str_to_date? what's wrong with my code?
thanks
The order by should come after the where clause
Try this::
SELECT *,STR_TO_DATE(detail,'%Y-%b')
FROM table2 JOIN user_table
ON table2.user_id = user_table.id
WHERE table2_col = 11
ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC
;
The following SQL code comes back with everything selected being NULL and the event_size being 0. My tables are set up correctly for the following criteria. What am I doing wrong here? Thank you for your help
SELECT table_one.event_id AS event_id, table_one.event_name AS event_name, table_one.event_address AS event_address, COUNT( table_two.user_id ) AS event_size
FROM table_one
JOIN table_two
ON table_one.event_id = table_two.event_id
WHERE (
table_one.event_start_date = '5/10/2012'
OR table_one.event_mid_date = '5/10/2012'
OR table_one.event_end_date = '5/10/2012'
)
ORDER BY event_size DESC
Try adding a group by clause:
group by table_one.event_id, table_one.event_name, table_one.event_address
This should do what you want.
I need to get a list of Articles sorted by the latest Comment from a related table joined on article.id = message.article_id using Kohana ORM. I managed to build a query that GROUPS and only then ORDERS:
SELECT *
FROM `articles`
LEFT JOIN `comments` ON ( `articles`.`id` = `comments`.`article_id` )
GROUP BY `comments`.`item_id`
ORDER BY `datetime` DESC
The query I am trying to build is:
SELECT * FROM `articles` LEFT JOIN
(SELECT article_id, MAX(datetime) as datetime FROM comments GROUP BY (article_id))
AS b ON `articles`.`id` = b.`article_id`
ORDER BY datetime
I have no idea how to rewrite it into Kohana ORM... (and I can't avoid ORM because there is a ton of code that depends on it)
$subquery = DB::select('article_id', array('MAX("datetime")','datetime'))
->from('comments')
->group_by('article_id');
$s = ORM::factory('article')
->join(array($subquery, 'b'), 'LEFT')
->on('article.id','=','b.article_id')
->order_by('datetime')
->find_all();
This is the translation of your query, I'm not really sure if it will work
ORM::factory('article')->join('comments', 'LEFT')->on('article.id', '=', 'comments.article_id')->group_by('comments.id')->order_by('date', 'DESC')->find_all()->as_array();
This generates sql as :
SELECT article.* FROM articles AS article LEFT JOIN comments
ON (article.id = comments.article_id) GROUP BY comments.id
ORDER BY date DESC
which matches your first query.
I'm not sure how to use nested query in ORM but there is Query builder in kohana which should do the trick for you.