Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subSelect function Enhancement #107

Open
simp-lee opened this issue Jun 17, 2022 · 2 comments
Open

subSelect function Enhancement #107

simp-lee opened this issue Jun 17, 2022 · 2 comments

Comments

@simp-lee
Copy link

I want sql:

SELECT articles.title,articles.id,articles.channel_id
FROM articles
INNER JOIN articles_tags ON articles.id = articles_tags.articles_id
INNER JOIN tags ON articles_tags.tags_id = tags.id
WHERE tags.id IN (
     SELECT tags.id
     FROM tags INNER JOIN articles_tags ON tags.id = articles_tags.tags_id
     WHERE tags.is_hidden = 1 AND articles_tags.articles_id = 111
     LIMIT 12
)
GROUP BY articles.id
ORDER BY articles.created_at DESC

This is what I did:

// other codes

$otherIds = $db->table('tags')->select('tags.id')
                          ->innerJoin('articles_tags', 'tags.id', 'articles_tags.tags_id')
                          ->where('tags.is_hidden', '=', '1')
                          ->where('articles_tags.articles_id', '=', 111)
                          ->limit(12)
                          ->getAll('array');

$db->table('articles')->select('articles.title,articles.id,articles.channel_id')
      ->innerJoin('articles_tags','articles.id','articles_tags.articles_id')
      ->innerJoin('tags','articles_tags.tags_id'_id','tags.id')
      ->in('tags.id',$otherIds)
      ->groupBy('articles.id')
      ->orderBy('articles.created_at', 'DESC')
      ->getAll();

// other codes

Is there any method similar to subSelect? Or any other suggestions?

$db->table('articles')->select('articles.title,articles.id,articles.channel_id')
      ->innerJoin('articles_tags','articles.id','articles_tags.articles_id')
      ->innerJoin('tags','articles_tags.tags_id'_id','tags.id')
      ->in(
           'tags.id',
           subSelect(
                    $db->table('tags')->select('tags.id')
                      ->innerJoin('articles_tags', 'tags.id', 'articles_tags.tags_id')
                      ->where('tags.is_hidden', '=', '1')
                      ->where('articles_tags.articles_id', '=', 111)
                      ->limit(12)
                      ->getAll('array')
           )
       )
      ->groupBy('articles.id')
      ->orderBy('articles.created_at', 'DESC')
      ->getAll();
@izniburak
Copy link
Owner

Hi @Lee-php ,
There is no support using subquery for now but I'll add it to ToDO list.
Now, you can use query method and write pure SQL code in order to run your query.

Thanks.

@simp-lee
Copy link
Author

OK, thanks izniburak.
Wait for your improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants