How to randomize the sql result set (column)? - mysql

String q="insert into ChooseRandom (Qid,Question,Option1,Option2,Option3,Option4,Answer,isImage,ImageQuestion) SELECT Qid,Question,Option1,Option2,Option3,Option4,Answer,isImage,ImageQuestion FROM model ORDER BY RAND()";
I am using the above query for randomize sql result set and insert the randomized data's into new table. this works good but this query randomize the rows in table. i need to randomize the rows as well as column and insert into new table.
How can i do this can any one help me to fix this
Thanks in advance
compare to image 1 and image 2 options are randomized as well as rows are randomized.
how can i achieve this

Actually my problem is i can randomize my database table data using rand() function in mysql. but i need to shuffle my column's all so.
i solve my issue but using array list in java i just add the column values to list and than i shuffle the list than i get the list item one by one the code what i used is given bellow .
List<String> lst = new ArrayList<String>();
op1=rs.getString("Option1");
op2=rs.getString("Option2");
op3=rs.getString("Option3");
op4=rs.getString("Option4");
lst.add(op1);
lst.add(op2);
lst.add(op3);
lst.add(op4);
Collections.shuffle(lst);
op1=lst.get(0);
op2=lst.get(1);
op3=lst.get(2);
op4=lst.get(3);
than i used this values to what ever i want.

Related

Laravel: Storing an array of 3 strings inside one column of a table

I am wondering what the best way to accomplish this would be. I need to store an array such as:
['hello', 'world', 'love']
Inside one column in a table. I do not want to create a new table where each value in the array becomes a separate row. Is there a better way? I guess my biggest concern is performance.
So when creating the migration for this column, ideally I would insert an array with 3 default/nullable values. Then I can set actual values for the array when needed in my application.
Simple scenario: I have a table called desk and I want to store 3 items that are on top of the desk such as ['pencil', 'ruler', 'stapler'], but I do not want to create a separate table desk_items to have a row for each item. Hope this makes sense.
Using Laravel and MYSQL
Thank you.
make desc table as jsonb format
$table->jsonb('desk');
and in model use casts
protected $casts = [
'desk' => 'array',
];
If you want to store the array in Database table you should send it with json_encode .
$data = new Data;
$data->Name = json_encode($request->DataName);
$data->save();
Thus, you can store an array in the name column of your data table.
to use this data you need to call the data with json_decode.
$data = json_decode(Data::find(1)->Name);

Check whether any values in array match any values in json column

I have a problem of checking whether any values in an array match any values in json column which contains an array with a name.
suppose i have an array [25,36,45,52] and json column is {"values": [25,24,15]}.
I want to check whether any values in array match any of values in json column in xampp mysql. please provide a better solution of doing this. this image show table structure of my database
i have 4 tables.
user
profile
profile
jobs
user table (id,userid)
jobs table (id,user_id,skill_id)
skill table (id,job_id,)
profile table (id,user_id)
now i want to search all jobs that match some or at least one skills.
i have tried with this but this is giving all jobs with out skills filtered.
$jobs = Job::with(['user','profile'])->with(['skills' => function($query){
$query->whereJsonContains('skills->skills',[35]);
}])->where('jobs.is_completed',0);
please help me.
you can use where Clause easily for example you would like to get rows that match skills 35,54:
$users = DB::table('table')
-> whereJsonContains('skills->skills', [35,54])
->get();
for more details about how to querying json column check official docs :
https://laravel.com/docs/5.8/queries#json-where-clauses

Rails, MySql, JSON column which stores array of UUIDs - Need to do exact match

I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Code igniter large query results result_array() result

I am looking at this.
I have queries that return up to 10,000 rows and usually fills up the memory
I am currently doing:
$this->db->get()->result_array();
Is there a way to not load all of the data into memory and use some sort of cursor? It seems result and result array are both arrays. (one is array of objects other is an array of arrays)
If you are using Active Record, which I would personally recommend, limit() should achieve what you are looking for.
$this->db->limit();
Can also use in this way, slightly easier and less lines of code:
$query = $this->db->get('Table_Name', 50); //syntax db->get('Table',limit_val);
return $query->result();
Also can return a limit with an offset:
$this->db->limit(10, 20); // Second parameter lets you set a result offset
Link for more help on Active Record Query's
https://www.codeigniter.com/userguide2/database/active_record.html#select
Mysql offset is the pointer you are looking for.
You can use it like:
$this->db->get(tableName,10,20);
The second parameter and the third one help you to set limit and offset .
Here are more details

SQL Alchemy return list of ids

I am using SQL Alchemy and I want to return a list of Document Ids. The Ids are the primary key in the documents table. My current query returns a list of tuples.
userDocs = session.query(Document.idDocument).filter(Document.User_idUser == user.idUser).all()
The reason I want a list of ids is so that I can search another table using in_(userDocs).
So another solution would be to be able to search using tuples. I am currently returning nothing from my second query using userDocs.
Thank you!!
You don't need to do an intermediate query, you can do this all in one shot!
things = session.query(Things) \
.join(Thing.documents) \
.filter(Document.User_idUser==user.idUser)
You just query on the properties of the Document through its relationship() on the intended entity.