I have four tables: followers, users, mixes, songs I am trying to get all the mixes from all the followers of one user, I have that part figured out, but I also want to get the songs from each of those mixes, currently my query is giving me results but each result is for one song on the mix, rather than an array of songs within each result for one mix ... any help would be amazing, my sql skills aren't the greatest and I have spent a lot of time trying to figure this out!
my current query is:
SELECT followers.following_id, users.id, users.user_username, mixes.id, mixes.mix_created_date, mixes.mix_name,songs.song_artist
FROM followers, users, mixes,songs
WHERE followers.user_id = 46
AND users.id = followers.following_id
AND mixes.user_id = followers.following_id
AND mixes.id > 0
ORDER BY mixes.mix_created_date DESC
LIMIT 10
the current result is (from running this through a cakephp custom query)
Array
(
[0] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[song_artist] => Yo La Tengo
)
)
[1] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[song_artist] => Animal Collective
)
)
as you can see the mix id's are the same, I am trying to get the songs to be an array inside of each result like :
Array
(
[0] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[0]=>array(
['song_artist'] => Yo La Tengo
),
[1]=>array(
['song_artist'] => Animal Collective
)
)
)
Really hoping this can be done with just one sql statement! thanks in advance!
You can use the SQL join command to make multiple queries together..
Use this...
sql_join
first a note: it looks like you have a missing condition. according to the above query, every song in songs table will be joined with every result possible. probably there should be a condition similar to the following added: (column names can be different based on your tables):
...
and mix.song_id=songs.song_id
...
as for your question: I don't know php so i regard mysql alone: I don't think it is possible to do it with mysql. mysql returns rows in the result set and each row can contain a single value in each column. to add a group of values (song names) in one column, they must be concatenated (and that is possible: Can I concatenate multiple MySQL rows into one field?), and later you split them back in your php script. this is not a good idea as you will need to choose a separator that you know will never appear in the values that are concatenated. therefore I think its better to remove the songs table from the query and after getting the mix id, run a second query to get all songs in that mix.
Related
I have a web project that is under development. I want to select data from database by group having a condition. But the problem is CodeIgniter doesn't give me the actually expected result. For reference, I am sharing code below.
First query
$this->db->select('serial, wardno, COUNT(serial) as total');
$this->db->where('status','active');
$this->db->group_by('wardno');
$data['wards'] = $this->db->get('certi_charter_inherit')->result_object();
First query give doesnot give me the targated result. It generates result.
[wards] => Array
(
[0] => stdClass Object
(
[serial] => 6
[wardno] => 2
[total] => 2
)
)
This is not my expectation. But If I use another direct query then it works clearly.
Second Query
$data['wards'] = $this->db->query("SELECT serial, wardno, COUNT(serial) as 'total' from certi_trade WHERE status='active' GROUP by wardno")->result_object();
[wards] => Array
(
[0] => stdClass Object
(
[serial] => 3
[wardno] => 1
[total] => 2
)
[1] => stdClass Object
(
[serial] => 6
[wardno] => 2
[total] => 1
)
)
The second query generates the result like below. And it is correct. I have tested my phpmyadmin console. So I confused which one should be used. I like the first one.
Any help is highly appriciated.
Please change your table name here as per your second query :
$data['wards'] = $this->db->get('certi_trade')->result_object();
My array is like:
Array (
[show_to_mentees] => 1
[show_profile] => 1
[job_title] => title
[mentor_org] => orgnization
[mentor_gradyear] => 2010
[mentor_sector] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[mentor_interest] => Array (
[0] => 2
[1] => 4
)
[mentor_degree] => 14
[career_summary] => summary
)
I have to insert the bold array values into a MySQL database, and that should be easily searchable.
Either I should insert a new row for each index or create a string of index values and save it in a single row.
I assume that the information in the array is about the 'mentor' itself and the key 'mentor_interest' show the interest of mentor in different sectors.
If above statement is right then, I assume/suggest that you have 'sectors' table in the database.
I would recommend separate tables mentor_sectors and mentor_interests. Also you would need to add each value separately with the mentor_id in another column for linking purpose.
I'm querying my database using aggregation and pipeline, with two separate queries:
$groups_q = array(
'$group' => array(
'_id' => '$group_name',
'total_sum' => array('$sum' => 1)
)
);
$statuses_q = array(
'$group' => array(
'_id' => '$user_status',
'total_sum' => array('$sum' => 1)
)
);
$data['statuses'] = $this->mongo_db->aggregate('users',$statuses_q);
$data['groups'] = $this->mongo_db->aggregate('users',$groups_q);
And I'm getting what I want:
Array
(
[statuses] => Array
(
[result] => Array
(
[0] => Array
(
[_id] => Inactive
[total_sum] => 2
)
[1] => Array
(
[_id] => Active
[total_sum] => 5
)
)
[ok] => 1
)
[groups] => Array
(
[result] => Array
(
[0] => Array
(
[_id] => Accounting
[total_sum] => 1
)
[1] => Array
(
[_id] => Administrator
[total_sum] => 2
)
[2] => Array
(
[_id] => Rep
[total_sum] => 1
)
)
[ok] => 1
)
)
I don't want to query my database twice. Is there is a better way to do it?
How can I accomplish it with one query? Should I use $project operator?
You can't use a single aggregate() to do two grouped counts with your desired result format. Once the data has been grouped the first time you no longer have the details needed to create the second count.
The straightforward approach is to do two queries, as you are already doing ;-).
Thoughts on alternatives
If you really wanted to get the information in one aggregation query you could group on both fields and then do some manipulation in your application code. With two fields in the group _id, results are going to be every combination of group_name and status.
Example using the mongo shell :
db.users.aggregate(
{ $group: {
_id: { group_name: "$group_name", status: "$status" },
'total_sum': { $sum: 1 }
}}
)
That doesn't seem particularly efficient and lends itself to some convoluted application code because you have to iterate the results twice to get the expected groupings.
If you only wanted the unique names for each group instead of the names + counts, you could use $addToSet in a single group.
The other obvious alternative would be to do the grouping in your application code. Do a single find() projecting only the group_name and status fields, and build up your count arrays as you iterate the results.
I am wondering what is better to do. I have a pulled back a query like this:
Array
(
[_id] => MongoId Object
(
[$id] => 4eeedd9545c717620a000007
)
[field1] => ...
[field2] => ...
[field3] => ...
[field4] => ...
[field5] => ...
[field6] => ...
[votes] => Array
(
[whoVoted] => Array
(
[0] => 4f98930cb1445d0a7d000001
[1] => 4f98959cb1445d0a7d000002
[1] => 4f88730cb1445d0a7d000003
)
)
)
Which would be faster:
Pull that entire array in 1 query and use in_array() to find the right id?
Pull everything from the first query except the votes and then do another mongodb query to see if that id exist in the array?
It Depends on a lot of factors that I suggest you test but IMO most of the time it would be faster to just do 2 querys
Depends on the size of the array being returned / searched.
Also different servers are doing the work, what do you mean by faster? At what scale?
I have the following array containing a master record of training courses held on a system (select_group just identifies related course groups):
Array
(
[DE00041-1] => Array
(
[select_group] => 1
)
)
The training course ids are then queried against a table containing only running courses whilst also grabbing extra information.
As one training course may run more than once per year they have a running_id which gives the following:
Array
(
[DE00041-1] => Array
(
[title] => xxx
[405] => Array
(
[start_quarter] => 1
[end_quarter] => 1
)
)
)
What I then need to do is add the original array select_group data into the newly created array.
The end result should be:
Array
(
[DE00041-1] => Array
(
[title] => xxx
[select_group] => 1
[405] => Array
(
[start_quarter] => 1
[end_quarter] => 1
)
)
)
I have looked over other SO / Google answers but couldn't find what I was looking for.
There may be 1-20 training courses listed, each one running once to four times per year.
foreach($yourArray as $key => $value){
$yourArray[$key]['select_group'] = $select_group_array[$key]['select_group'];
}
Another variant:
$result = array_merge_recursive($arr1, $arr2);