Laravel mongodb where array field contains value - mysql

I use mongodb and mysql together with Laravel.
Fields table in mysql database can have more options in mysql database.
fields table in mysql: id, name
options table in mysql: id, field_id, name
so in mongodb database I have adverts table and this adverts table have field and field equal to options
something like this for example {field_id_1: [option_id, option_id]}. When I use it like this there is no problem when querying the database. It is simple like this $advert->whereIn('field_id_1', $request->options).
but then I decided why not to keep this options as an array in mongodb and without field_id like this {options: [option_id, option_id, option_id]}. So there is the question now I somehow want query the database where this options field contains requests value something reverse of $adverts->whereIn('field', $options)
I know a way but not sure
$adverts->where('options', 'LIKE', '%"'.$option_id.'"%');

I don't know if this answer will work for you :
$result = $adverts->where("options", "all", [$option_id])->get();
or :
$result = $adverts->where("options", "all", "$options_ids")->get();
where $options_ids are an array of options

Related

get distinct "title" in mysql in django

I have used django to develop a web app.
I want to get the distinct "title" form the queryset get by filter.
But I use mysql so could not pass "title" to distict.
How could I filter the queryset with the distinct "title"?
query_set = CourseInfo.objects.filter(discipline_id=id).distinct('title')
return render(request, 'main.html',
context={'query_set':query_set})
I get error for this in mysql as it may only used in postgresql
`
It will give you distinct titles:
titles = CourseInfo.objects.filter(
discipline_id=id
).order_by('title').values('title').distinct()
Note:
there is no such thing called SELECT DISTINCT ON in MySQL.
You can only use it in Postgresql but maybe GROUP BY helps you for converting SELECT DISTINCT ON query to MySQL query.
Check out this link then you kinda can convert this query to MySQL query.

Database - getting multi dimensional data

Is there some command in mysql or nosql databases so i could get data sorted in multidimensional array by some sort field?
For example : SELECT * FROM TABLE ARRAYSORT field1
so we would get
array('field1_value1'=>array(data),'field1_value2'=>array(data))
or we must process data in our programming language to get array like this.
It is just imposible that no database support query like this.
I don't know if that's what you want to do:
PHP:
$result = $polaczenie->query($sql)
while ($row = $result->fetch_array()){
$array[$Row['firstColumnToSort']][$Row['secondColumnToSort']]=data;
}
Or something like that

Django mysql count distinct gives different result to postgres

I'm trying to count distinct string values for a fitered set of results in a django query against a mysql database versus the same data in a postgres database. However, I'm getting really confusing results.
In the code below, NewOrder represents queries against the same data in a postgres database, and OldOrder is the same data in a MYSQL instance.
( In the old database, completed orders had status=1, in the new DB complete status = 'Complete'. In both the 'email' field is the same )
OldOrder.objects.filter(status=1).count()
6751
NewOrder.objects.filter(status='Complete').count()
6751
OldOrder.objects.filter(status=1).values('email').distinct().count()
3747
NewOrder.objects.filter(status='Complete').values('email').distinct().count()
3825
print NewOrder.objects.filter(status='Complete').values('email').distinct().query
SELECT DISTINCT "order_order"."email" FROM "order_order" WHERE "order_order"."status" = Complete
print OldSale.objects.filter(status=1).values('email').distinct().query
SELECT DISTINCT "order_order"."email" FROM "order_order" WHERE "order_order"."status" = 1
And here is where it gets really bizarre
new_orders = NewOrder.objects.filter(status='Complete').values_list('email', flat=True)
len(set(new_orders))
3825
old_orders = OldOrder.objects.filter(status=1).values_list('email',flat=True)
len(set(old_orders))
3825
Can anyone explain this discrepancy? And possibly point me as to why results would be different between postgres and mysql? My only guess is a character encoding issue, but I'd expect the results of the python set() to also be different?
Sounds like you're probably using a case-insensitive collation in MySQL. There's no equivalent in PostgreSQL; the closest is the citext data type, but usually you just compare lower(...) of strings, or use ILIKE for pattern matching.
I don't know how to say it in Django, but I'd see if the count of the set of distinct lowercased email addresses is the same as the old DB.
According to the Django docs something like this might work:
NewOrder.objects.filter(status='Complete').values(Lower('email')).distinct()

optionally use table prefix in codeigniter using active records

I want to produce sql like:
select id, "file_name.png" from prefix_table;
In CI, by using active records, I code that with:
$this->db->select('id, "file_name.png"', FALSE)->from('prefix_table');
but what I got is:
select id, prefix_"file_name.png" from prefix_table;
Is there any way to use the table prefix optional? Or may be, how do I do not use the prefix when selecting using active records?
This is a limitation/bug in CodeIgniter right now -- you won't be able to use a string like that without the prefix butting in. I'd suggest writing the query manually. I've opened an issue on Github for it.
Also, you should use an AS column definition when selecting a string like that, otherwise the string name will also be the column name, and you'll end up with something like:
array(
'id' => 2,
'file_name.png' => 'file_name.png'
)

Unexpected field in Mysql fetch data

I have a query in which the data is lifted from the database.
Eg: I have Category as a field which has values like Doctors, Pathologists etc which is taken from the database.
Category
--------
Doctors
Pathologists
Pediatrics/Child Specialist --->> Unwanted data
The problem is there is a data(value) in 'Category' field which is unexpectedly showing in the list which is not in my database. I even tried to drop the table entirely and reload it but that field is still showing.
Can anyone tell me why is this happening?
Additional Info
function getTierCategory()
{
echo "</br>";
$result = mysql_query("SELECT DISTINCT Category FROM doctors order by Category asc")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['Category'].'">'.$tier['Category'].'</option>';
}
}
I have tried renaming the field but how can I if this value is not even in my database.
My Database looks like
Category | Qualification | State | District
--------------------------------------------
and so on
Log in to your database in PHPmyadmin or something like that and check results of your mysql query.
In some complex queries you can receive redundant fields (on example indexes when you have counting variable in PHP) or NULL values.
mysql_fetch_array() returns results in two ways:
number indexed array ($array[0] etc)
associative array ($array['field_name'] etc)
Try with mysql_fetch_assoc or mysql_fetch_row instead. Also PDO_MYSQL API is recommended to use for MySQL querying. It has more convenient form.