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
Feature Request: where style clauses within join #2707
Comments
The current solution, which admittedly is very ugly is to do:
We should probably add the concept of a where clause to the join object, so you could do:
and it would be output as: select ....
left join `wp_postmeta` as `price` on `p`.`id` = `price`.`post_id`
and `price`.`meta_key` = "_regular_price"
... |
Wouldn't it make more sense with something like It just sounds a bit weird to have |
In 0.15.0 there is also helper for being able to write |
I think in this case he wanted the actual string '_regular_price' so it should be Edit: My bad, I'm thinking postgres.. Perhaps "_regular_price" is valid in mysql.. |
@wubzz yeah I think |
I started playing around with this a bit: Builder qb()
.select({
id: 'p.ID',
status : 'p.post_status',
name: 'p.post_title',
// type: 'terms.name',
price: 'price.meta_value',
createdAt: 'p.post_date_gmt',
updatedAt: 'p.post_modified_gmt'
})
.from({p:'wp_posts'})
.leftJoin({price: 'wp_postmeta'},
function() {
this
.on('p.id', '=', 'price.post_id')
.onVal(function() {
this.onVal('price.meta_key', '_regular_price')
.andOnVal('price_meta_key', '_regular_price')
})
.orOnVal(function() {
this.onVal('price_meta.key', '_regular_price')
})
}) Output select "p"."ID" as "id", "p"."post_status" as "status", "p"."post_title" as "name", "price"."meta_value" as "price", "p"."post_date_gmt" as "createdAt", "p"."post_modified_gmt" as "updatedAt" from "wp_posts" as "p" left join "wp_postmeta" as "price" on "p"."id" = "price"."post_id" and ("price"."meta_key" = ? and "price_meta_key" = ?) or ("price_meta"."key" = ?) What I don't really like is having to call |
Closing, feature merged towards next version. #2746 |
Environment
Knex version: latest
Database + version: MySQL 5.7.x
OS: Ubuntu 14.04
When I use this:
When I execute this query I get this error:
I need to use a string here not a column for join
The text was updated successfully, but these errors were encountered: