In SQL
DECLARE #Search VARCHAR(100) = ''
SELECT * FROM Customers WHERE #Search = '' OR FirstName like '%'+ #Search +'%'
Please help me out on this query, how to write this query in mongoDB
Well you will have to use $regex operator in mongodb in order to achieve that
your query would be some what like this
db.customers.find({"FirstName": /.*SearchTerm.*/})
Related
I am using PostgreSQL to create complexe dynamic queries. In my queries I can use one variable multiple times in multiple positions in the query, to simplify things, I want to know if I can do something like this :
SELECT * FROM employees Where name = $1 and id = $2 and manager_id = $2;
And then execute the query like this :
EXECUTE format ('SELECT * FROM employees Where name = $1 and id = $2 and manager_id = $2;') using (var_name, var_id);
The parameters are positional, so $1 always refers to the first parameter regardless where and how often you refer to it.
But you must not enclose the parameters in parentheses because that creates an anonymous record. using (var_name, var_id) passes a single parameter (which is a record with two fields).
So you can use:
EXECUTE format('SELECT * FROM employees Where name = $1 and id = $2 and manager_id = $2')
using var_name, var_id;
Suppose my table user_info has 2 columns, one is #username and another one is #info.
Now I already made a query "INSERT INTO user_info(username) value('')
How can I make another query to put data on column #info for the same username?? Because after the first query I'll have null for the column #info I believe.
Just to clarify, I don't get the #info when I have the username. Each user will get their info later. So I can't put then on the same query.
In that case, you need to update the row, simply using update on where the username exist
From documentation:
UPDATE table_name
SET column1 = value1,
column2 = value2,
...
WHERE condition;
Your case:
UPDATE user_info
SET info='new_information'
WHERE username='existing_username'
Please Help.
I am trying to create the query in mongodb like (in MySql):
SELECT * FROM user WHERE user_id = '1' AND (type = 'admin' OR type = 'requester') ORDER BY user_id DESC
How do I convert this query into mongodb find({})?
I'd use $in instead of $or since only a single field is involved:
db.user.find({
user_id:"1",
type: {$in:["admin","requester"]}
}).sort({"user_id":-1});
Check this,
db.user.find({user_id:"1",$or:[{"type":"admin"},{"type":"manager"}]})
I'm trying to figure out how to give a column an alias using Eloquent.
So, in other words, how do I execute the following mysql query using Eloquent?
SELECT occupation AS test FROM users WHERE occupation = 'PIMP';
Thx in adv!
Eloquent returns a regular Fluent query. So you can try something like this (assuming your model name is 'User'):
$user = User::where_occupation('pimp')->get(array('occupation as test'));
This is how I have been able to do this in Laravel 5 using select() and passing the col name and the alias to that method (here I'm also using groupby() to restrict it to "DISTINCT" return values then using toarray() to return any array instead of a Collection Object:
$results = asset::select('model_code__c AS option')->whereRAW("model_code__c <> '' AND status = 'A'")->groupby('model_code__c')->get()->toarray();
right now I have a keyword array like:
['key1', 'key2', 'key3'.......] , the keyword can be number or character.
If I want to search in my table (postgres database), and find out all record contain any of keyword in that array, how can I do that?
For example:
I got a table which has a column called name and a column called description
I need find all record that either name or description contains any keywords in that array.
thanks
Maybe this example will be useful:
CREATE TABLE TEST(
FIELD_KEY TEXT);
INSERT INTO TEST VALUES('this is hello');
INSERT INTO TEST VALUES('hello');
INSERT INTO TEST VALUES('this');
INSERT INTO TEST VALUES('other message');
SELECT *
FROM TEST
WHERE FIELD_KEY LIKE ANY (array['%this%', '%hel%']);
This will return:
this is hello
hello
this
Here other example:
SELECT *
FROM TEST
WHERE FIELD_KEY ~* 'this|HEL';
~* is case insensitive, ~ is case sensitive
You can try this example here.
select *
from t
where
array[name] <# my_array
or array[description] <# my_array
Couple the like operator with the any subquery expression:
select *
from t
where name like any (values ('%John%'), ('%Mary%'))
Or the array syntax:
where name like any (array['%John%', '%Mary%'])