let duplicates = await Books.query()
.select('book_id')
.groupBy('book_id')
.having(knex.count('book_id'), '>', 1);
Here I want to use the count aggregator inside the having clause. How can I do that inside knex.having() function?
Using a small snippet of knex.raw is probably the easiest way.
let duplicates = await Books.query()
.select('book_id')
.groupBy('book_id')
.having(knex.raw('count(book_id) > 1'));
Or if you want to properly escape your column name with backticks you can use a ?? positional argument
let duplicates = await Books.query()
.select('book_id')
.groupBy('book_id')
.having(knex.raw('count(??) > 1', 'book_id'));
Related
I’m a beginner in Laravel but have a problem at first. I wrote this query and I’m waiting for Sonya Bins as result but unexpectedly I see ["Sonya Bins"]. what’s the problem?
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
return view('products',compact('articles'));
});
pluck will return array if you want to get only single value then use value
// will return array
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
//will return string
$articles=DB::table('users')->where('id','2')->value('name');
// output Sonya Bins
here is an example from the documentation:
if you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
Read more about it here
Hope it helps.
Thanks
pluck() used to return a String before Laravel 5.1, but now it returns an array.
The alternative for that behavior now is value()
Try this:
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->value('name');
return view('products',compact('articles'));
});
I think it's easier to use the Model + find function + value function.
Route::get('products', function () {
$articles = User::find(2)->value('name');
return view('products',compact('articles'));
});
pluck will return the collection.
I think id is your primary key.
You can just get the first record, and call its attribute's name:
DB::table('users')->where('id','2')->first()->name;
or
DB::table('users')->find(2)->name;
First thing is that you used invalid name for what you pass to view - you don't pass articles but user name.
Second thing is that you use get method to get results instead of first (or find) - you probably expect there is only single user with id = 2.
So to sum up you should use:
$userName = DB::table('users')->find(2)->name;
return view('products',compact('userName'));
Of course above code is for case when you are 100% sure there is user with id = 2 in database. If it might happen there won't be such user, you should use construction like this:
$userName = optional(DB::table('users')->find(2))->name;
($userName will be null if there is no such record)
or
$userName = optional(DB::table('users')->find(2))->name ?? 'No user';
in case you want to use custom string.
This is my code:
let sortArray = array1.sort((a,b)=> a.id - b.id);
let array1Ids: number[] = [];
sortArray.map(resp => array1Ids.push(resp .id));
I want to make this code more optimized, how do I refactor it?
My goal is just to sort and get IDs.
let sortedArrayIds = array1.sort((a,b) => a.id - b.id).map(element => element.id);
If you use map you should always put it on the right side of an expression, because map RETURNS the new array. If you want just an iteration use forEach.
I'm working on a command that searches maps for the game osu! on the site bloodcat.com and I made a way to filter the maps with:
.split('status=ranked').join('&c=b&s=1&m=&g=&l=')
.split('status=approved').join('&c=b&s=2&m=&g=&l=')
.split('status=qualified').join('&c=b&s=3&m=&g=&l=')
.split('status=loved').join('&c=b&s=4&m=&g=&l=')
.split('status=unranked').join('&c=b&s=0&m=&g=&l=')
.split('status=graveyarded').join('&c=b&s=0&m=&g=&l=');
Now someone can do !search 'map name' status=unranked and that would look for https://bloodcat.com/osu/?mod=json&q='map name'&c=b&s=0&m=&g=&l= but if someone does for example !search status=unranked 'map name' that doesn't work is there any way.
Can I make the status=unranked at the end even if the user doesn't put it in the end in the command?
I would do it like this: first, check if the string of arguments starts with status=. If so, you'll need to switch them, otherwise, it should work as it's doing now.
In order to switch those, I would split the string with spaces, remove the first argument (status=*), rejoin that with spaces and add the removed part:
function getQueryURL(argStr = "") { // argStr should be like "'map name' status=unranked" OR "status=unranked 'map name'"
if (argStr.startsWith('status=')) {
let arr = argStr.split(' ');
let removed = arr.shift();
argStr = arr.join(' ').trim() + removed.trim();
}
let res = argStr.split('status=ranked').join('&c=b&s=1&m=&g=&l=')
.split('status=approved').join('&c=b&s=2&m=&g=&l=')
.split('status=qualified').join('&c=b&s=3&m=&g=&l=')
.split('status=loved').join('&c=b&s=4&m=&g=&l=')
.split('status=unranked').join('&c=b&s=0&m=&g=&l=')
.split('status=graveyarded').join('&c=b&s=0&m=&g=&l=');
return "https://bloodcat.com/osu/?mod=json&q=" + res;
}
I'm trying to process an array of JSON objects that have various common attributes, filtering each array entry on one or more of those attributes.
Normally, I'd just do it something like this:
let filteredResultsArray = originalArray.filter((obj) => {
return obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10;
});
My problem is that the filter parameters (the part after "return" in the code above) are highly variable (and unpredictable) from run to run, so I can't hard-code them in the filter. I compute them on the fly and store the whole thing in a string in my code. For example, on one run it might be:
myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
I've tried doing this:
let filteredResultsArray = originalArray.filter((obj) => {
return myAttributeString;
});
That's failing to filter anything. Apparently .filter() is not properly interpreting what I've stored in myAttributeString as filter criteria.
I have a sneaking suspicion that eval(myAttributeString) might be one way to pull this off, but unfortunately I'm working on a team where we've got tslint set to disallow the use of eval(), so that's not an option.
Anybody have an idea how I can get this to work?
When you "compute them on the fly", instead of creating a string, create a callback function that you can then pass to filter. For example, instead of
const myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
do
const filterCallback = obj => obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10
Then, later, when the appropriate time comes to .filter, simply pass that as the callback:
const filteredResultsArray = originalArray.filter(filterCallback);
If you can't pass functions around, another option would be to build an array of conditions, for example
[
{
prop: "attribute1",
constraint: "<=",
value: 3
},
{
prop: "attribute2",
constraint: ">",
value: 0
},
// ...
]
and then turn the object into the filter function needed.
****************************************UPDATE******************************
As I suspected, eval() did work, but since I can't use it in my delivered code, and thanks to CertainPerformance's suggestion (which put my thinking on the right track) as well as the Node.js documentation site (via a lucky Google search), I was able to find a workaround using the vm module:
import * as vm from "vm";
let filteredResultsArray = originalArray.filter(
vm.runInThisContext("(obj) => {
return " + myAttributeString + ";}"));
Case closed.
For my project, I extracted tweets from a CSV file in Swift. Problem is now all tweets are parsed as one element in an array, separated by ",".
let tweetsOfColumns = columns["tweet"]
let seperatedColumns = tweetsOfColumns.componentsSeparatedByString(",")
Error message: '[String]?' does not have a member named
'componentsSeparatedByString'.
I checked if tweetsOfColumns contains multiple elements, but it doesn't allow me to subscript with tweetsOfColumns[index].
Looking at the link you reference, columns["tweets"] is going to give you back an array of the values from the "tweets" column, so it's what you need already, there's no additional comma's to split things on, you just need:
let seperatedColumns = columns["tweet"]
to have an array containing the tweet column for each row.
When you try to get an element from a dictionary, like
columns["tweet"]
it will give you back an optional, because if there is nothing associated with the key, it gives you back nil (None), otherwise the value wrapped in an optional (Some(data)).
So you have to unwrap the optional for example:
columns["tweet"]!
You have to either use the optional ? to access the string:
let seperatedColumns = tweetsOfColumns?.componentsSeparatedByString(",")
But you should unwrap it:
if let unwrappedTweets = tweetsOfColumns?.componentsSeparatedByString(","){
let seperatedColumns = unwrappedTweets
}
The problem is probably that you'll get an optional back, which you have to unwrap. And the easiest and most elegant is to use the if-let unwrapper.
if let tweetsOfColumns = columns["tweet"] {
let seperatedColumns = tweetsOfColumns.componentsSeparatedByString(",")
// do something with the seperatedColumns
}
Based on David's question and the OP's response in the OP comments, you can use map on the Array returned by columns["tweet"]. Please post actual data/code in the future.
let columns = [
"tweet":["handleX,tag1,tag2,textA,textB",
"handleY,tag1,tag2,textC,textD"]]
var chunk = [[String]]()
if columns["tweet"] != nil {
chunk = columns["tweet"]!.map {
return $0.componentsSeparatedByString(",")
}
}