I've been working on a project where I have 3 tables of dates, times and usernames and when I join them the last view is:
same date - same time - diff username
same date - same time - diff username
diff date - diff time - diff username
An example with real data would be:
2014-01-03 | 10.08 | alexrussell
2014-01-05 | 11.15 | geartz
2014-01-05 | 14.23 | geartz
2014-02-02 | 13.44 | alexrussell
2014-02-02 | 13.44 | geartz
2014-02-02 | 18.21 | alexrussell
What I'm trying is to create a table where the data that's the same from row to row is grouped:
-----------------------------------
| Date | Time | Username |
|------------|-------|--------------|
| 2014-01-03 | 10.08 | alexrussell |
|------------|-------|--------------|
| | 11.15 | geartz |
| 2014-01-15 |-------|--------------|
| | 14.23 | geartz |
|------------|-------|--------------|
| | | alexrussell |
| | 13.44 |--------------|
| 2014-02-02 | | geartz |
| |-------|--------------|
| | 18.21 | alexrussell |
-----------------------------------
I have 1 object and I'm using foreach to parse it. I tried several selects with distinct but that way, I can't include other objects in table so code gives error like 1 select with distinct for date and 1 for the rest which have "where" statement. I tried 1 object with several arrays in it but again since I'm using foreach, I can only fill 1 column and next column requires other object.
I also tried giving a rowspan per object count but It didn't work since I can't exactly get the user count.
There must be a way and I'm totally stuck on this. Probably I'm not the first one encountering a problem like this.
Related
A lot of thought process went before I decided to post this question. Trying to explain my problem in a simplified format.
I have 2 tables in my mySQL table, one of which is the users and the other one is the questions and answers related to that user.
Simplified Example:
Users
| id | name | registered_on |
|----|--------|---------------------|
| 1 | Aaron | 2017-02-01 00:01:02 |
| 2 | Baron | 2017-02-01 01:01:02 |
| 3 | Chiron | 2017-02-01 02:01:02 |
Answer Keys
| id | user_id | keyword | value | created_on |
|----|---------|---------|---------|---------------------|
| 1 | 1 | gender | Male | 2017-02-01 00:01:02 |
| 2 | 1 | age | 24 | 2017-02-01 00:01:02 |
| 3 | 2 | gender | Male | 2017-02-01 00:01:02 |
| 4 | 2 | age | Unknown | 2017-02-01 00:01:02 |
| 5 | 3 | gender | God | 2017-02-01 00:01:02 |
I hope the relation above is clear. So what I wish to achieve is to create a CSV report like this
| name | gender | age | registered_on |
|--------|--------|---------|---------------------|
| Aaron | Male | 24 | 2017-02-01 00:01:02 |
| Baron | Male | Unknown | 2017-02-01 01:01:02 |
| Chiron | God | NULL | 2017-02-01 02:01:02 |
As my research suggests, this can be done in the following ways :
Prepared Statements (Cannot use because CodeIgniter does not Support)
Paging (The vertical table is the problem)
MySQL Pivot Tables (But with Dynamic column names -- feels complicated!)
Any other better way that I do not know of
I am thinking about paging but am yet to figure out how it could be
used in the case of vertical tables. I would like it if any of you guys have faced the same problem or have some meaningful suggestions! Any help is appreciated!
If I understand well your problem, you want a CSV file with all user informations available using the answer keys as columns?
Why not doing this with 2 queries?
First, you can select all different keys in your answer table (select distinct keyword...)
Then, create your query with joins looping on your results :
$this->db->select('u.id, u.name, u.registered_on');
$this->db->from('users u');
foreach($keywords as $i_key => $s_keyword){
$this->db->join('answers_table at'.$i_key, 'at'.$i_key.'.user_id = u.id', 'left');
$this->db->select('at'.$i_key.'.value as '.$s_keyword);
}
$a_users = $this->db->get()->result();
I am a newbie to redis.
Here is my doubt.
Let's say I have 100 records from a MySQL table.
id | name | surname | age | username | password | amount | currency
1 | nick | goean | 29 | nick_go | nickpass | 120
2 | joe | keve | 30 | keve_joe | kevepass | 110
I need to store this in redis and should use to query by age and currency,
actually I should get all the possible combinations to query from redis.
Can anyone help me with an example data structure for the same?
Okay so I'm creating a task manager for my company. A user can assign assign a task to multiple other users. So I've though of 2 ways of implementing this.
This is my tasks table for option one (at least the columns that are important in this discussion ):
----------------------------------------------
| id | assigned_to | assigned_from |
---------------------------------------------
| 1 | 1,3,6 | 4 |
--------------------------------------------
| 2 | 1,4 | 2 |
---------------------------------------------
So here I pretty much just comma separate each user_id that is assigned to this particular task
Option 2:
----------------------------------------------------------
| id | task_id | assigned_to | assigned_from |
------------------------------------------------------------
| 1 | 335901 | 1 | 4 |
-----------------------------------------------------------
| 2 | 335901 | 3 | 4 |
-----------------------------------------------------------
| 3 | 335901 | 6 | 4 |
-----------------------------------------------------------
| 4 | 564520 | 1 | 2 |
-----------------------------------------------------------
| 4 | 564520 | 4 | 2 |
-----------------------------------------------------------
So as you can see here instead of putting the assiged_to is's here I just create a task id which is a random number and then I can groupBy 'task_id'. This is currently they way I have built it but for some reason it feels like it might screw me over in the future (not that option one doesn't give me the same feeling). So my question is which way do you guys recommend or is there maybe a different better way that I could be doing this?
Option 2 ist the better solution since you can acutally work with the table. You may e.g. create another table Tasks with
Task_id | Task_name | Budget | ...
Or a table with user-IDs for assigned_to and assigned_from. All these tables can be joined together if you use 2nd Option.
btw it is the correct normalization form
You can use Option 2 and normalize further if tasks are always assigned by/from the same person.
Tasks table:
task_id | assigned_from
1 | 4
2 | 2
The Assignees table then doesn't need to have the assigned_from since it's always the same for that task_id:
id | task_id | assigned_to
1 | 1 | 1
2 | 1 | 3
3 | 1 | 6
4 | 2 | 1
5 | 2 | 4
i have a table in following format:
id | title
---+----------------------------
1 | php jobs, usa
3 | usa, php, jobs
4 | ca, mysql developer
5 | developer
i want to get the most popular keywords in title field, please guide.
If you have a list of keywords, you can do the following:
select kw.keyword, count(*)
from t cross join
keywords kw
on concat(', ', t.title, ',') like concat(', ', kw.keyword, ',')
As others have mentioned, though, you have a non-relational database design. The keywords in the title should be stored in separate rows, rather than as a comma separated list.
If your data is small (a few hundred thousand rows or less), you can put it into Excel, use the text-to-columns function, rearrange the keywords, and create a new, better table in the database.
SELECT title 1, COUNT(*) FROM table GROUP BY title 1
EDIT
Since you've edited and presented a non-normalized table, I would recommend you normalize it.
Have a read of: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
You need to modify your database. You should have something like this:
items
+----+---------------+
| id | title |
+----+---------------+
| 1 | something |
| 3 | another thing |
| 4 | yet another |
| 5 | one last one |
+----+---------------+
keywords
+----+-----------------+
| id | keyword |
+----+-----------------+
| 1 | php jobs |
| 2 | usa |
| 3 | php |
| 4 | jobs |
| 5 | ca |
| 6 | mysql developer |
| 7 | developer |
+----+-----------------+
items_to_keywords
+---------+------------+
| item_id | keyword_id |
+---------+------------+
| 1 | 1 |
| 1 | 2 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 4 | 5 |
| 4 | 6 |
| 5 | 7 |
+---------+------------+
Do you see the advantage? The ability to make relations is what you should be leveraging here.
Newbie here.
I'm using codeigniter and mysql
How can I dynamically (number of names may change) convert table from:
+------+-------+-------+
| date | name | value |
+------+-------+-------+
| 06-01| A | 1 |
| 06-02| A | 2 |
| 06-02| B | 3 |
| 06-03| C | 4 |
+------+-------+-------+
To:
+------+---+---+---+
| date | A | B | C |
+------|---+---+---|
| 06-01| 1 | | |
| 06-02| 2 | 3 | |
| 06-03| | | 4 |
+------+---+---+---+
?
Thank you.
Something like this should work.
SELECT date,
SUM(IF(name='A',value,0)) AS 'A',
SUM(IF(name='B',value,0)) AS 'B',
SUM(IF(name='C',value,0)) AS 'C'
FROM myTable
GROUP BY date
ORDER BY date
You need to know what your column names could be to add each of the SUMs manually into your SQL statement, but you could do this using PHP if it was likely to change a lot.
Likewise, replace value with 1 if you just wanted a count of how many times each name appeared, rather than the total of the values in name.