Create MySQL View from Lookup Table and Actual Data - mysql

I would like to create a view from a lookup table and actual data. I have two questions.
How would you accomplish this?
Should I try to do it this way?
Senerio
Table Name: steps
Table structure with values:
There are other columns hence the ( ... )
| id | Name | ... |
| 1 | Step One | ... |
| 2 | Step Two | ... |
Table Name: steps_completed
Table structure with values:
| user_id | steps_id |
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
Results Wanted
View Structure and Values wanted:
| user_id | step_one | step_two |
| 1 | 1 | 0 |
| 1 | 1 | 1 |
Thanks for your help.

Sounds like you need a cross tab query. The only way I've seen it done is via a stored procedure like this: Cross Tab Query

Related

Speed up SELECT using ordered SQL table

Consider following table:
| ID | COL1 |
| -- | ---- |
| 1 | xxx |
| 3 | xxx |
| 2 | xxx |
| 2 | xxx |
| 3 | xxx |
| 2 | xxx |
| 1 | xxx |
Im using command:
SELECT * FROM table WHERE ID=id
Problem is that table contains about 500 000 000 rows with 1000 different IDs and command is slow. My idea is somehow sort table by ID:
Modified table:
| ID | COL1 |
| -- | ---- |
| 1 | xxx |
| 1 | xxx |
| 2 | xxx |
| 2 | xxx |
| 2 | xxx |
| 3 | xxx |
| 3 | xxx |
With sorted table it should be faster. My question is how keep table sorted? OR is there any other way how make it faster?
Is that all there is to the table? That is, if there are only those 2 columns, then simply add
INDEX(id, col1)
That way you will get the optimized lookup you desire. It still won't be fast -- it needs to shovel about 500K rows to the client.
If the table is more complex than you have shown, my suggestion may be invalid. At least provide the complete SHOW CREATE TABLE. I may need to suggest a rearrangement of the PK.

What is the best way to reduce MXN table search in MySQL

I have two MySQL tables called tasks and users. All I want to do is I don't want to display the tasks that is already done by a user in his panel. Suppose the task table has about 1000 entries and there are about 50000 users. Also the users and the tasks keep increasing.
One solution I can think of, 1st is creating a separate table of task x user size.
For example:
user table
+---------+--------+-------+
| user_id | fname | lname |
+---------+--------+-------+
| 1 | John | Smith |
| 2 | Steve | Mark |
+---------+--------+-------+
task table
+---------+-------------+---------------+
| task_id | task | task_duration |
+---------+-------------+---------------+
| 1 | Do task 1 | 1 hour |
| 2 | Do task 2 | 1 hour |
+---------+-------------+---------------+
Creating a separate table called display
+------------+---------+---------+
| display_id | task_id | user_id |
+------------+---------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
+------------+---------+---------+
So only listed tasks will be shown to the particular user.
The problem is that This does not look like an efficient solution. How can I design table in this scenario in an efficient way. If not what are the other ways?

MYSQL pivot table create

I've tried various tutorials and examples how to make a dynamic pivot table, and I cant make it work. Every time I am getting some sql error.
Can someone help me how to make dynamic pivot table. I am new, and I guess I will make a filter search by attributes easy when I make pivot table first. Here are my tables:
Table 1 name: table_items
------------------------------
| item_id | title | ... |
------------------------------
| 1 | item 1 | ... |
------------------------------
| 2 | item 2 | ... |
------------------------------
Table 2 name: table_item_options
-----------------------------
| option_id | option_name |
-----------------------------
| 1 | Color |
-----------------------------
| 2 | Size |
-----------------------------
Table 3 name: table_attributes
--------------------------------------------------
| attribute_id | option_id | attribute_name |
--------------------------------------------------
| 1 | 1 | Blue |
--------------------------------------------------
| 2 | 1 | Red |
--------------------------------------------------
| 3 | 2 | XL |
--------------------------------------------------
| 4 | 1 | Green |
--------------------------------------------------
| 5 | 2 | L |
--------------------------------------------------
Table 4 name: table_item_attributes
------------------------------------------------------
| assigned_id | item_id | option_id | attribute_id |
------------------------------------------------------
| 1 | 1 | 1 | 1 |
------------------------------------------------------
| 1 | 1 | 1 | 2 |
------------------------------------------------------
Any help is much appreciated
What I want is to make a product filter for items by its attributes. As I understood it is best possible to make a pivot table, and to avoid duplicate results bu joining tables.
All first columns are primary keys, and with autoincrement.
EDIT:
At this point, using inner join , I create one big table, and in Select clause I put " WHERE attribute_id = '2' ". (Which means Select all Red) , but like this I can use only one filter.
So I have a problem that I cant use more than one filter from attribute_id column, and I want to filter by more attributes (other color, other size, city, etc..)
So how can I do this using pivot table ? My intention is to dinamicly create columns which would be option.name from (table_item_options) and to populate it with attribute_id or attribute_name, so I can use more filters
for example:
-----------------------------------------------------
| item_id | ... | color | size | City | etc.. |
------------------------------------------------------
| 1 | ... | 1(or Red) | L | A | ... |
------------------------------------------------------
| 1 | ... | 2(or Blue)| XL | B | ... |
------------------------------------------------------
In table like this, If I Select * .. where color = red , I will be able to filter this table by another column, eg: Where city = a
I hope it is more clear now.

MySQL: Hundreds of tables or one big table?

I want to create a webpage where user can organize things they collect. As everyone collects something else I want to have the users create their own datamodell (with strict limitations). If two people are collection the same "things" they can share a datastructure.
My idea was to give every collection an ID and all the tables belonging to that collection will have the ID as a prefix.
Table: Collections
ID | Collection
1 | Poststamps
2 | Barbie Dolls
Table: 1_Base
ID | StampValue | StampPic
....
Table: 2_Base
ID | EAN | Pic
....
Thus I would create many tables as each user could in theory create their own collection. I could also use only one very big table and a mapping table. Example:
Table: Colleactions
ID | Collection
1 | Poststamps
2 | Barbie Dolls
Table: Mapping
fkCollection | FieldName | Mapping
1 | DoubleField1 | StampValue
1 | BlobField1 | StampPic
2 | StringField1 | EAN
2 | BlobField1 | StampPic
Table: CollectionData
fkCollection | DoubleField1 | ... | DoubleField10 | StringField1 | ... | Stringfield10 | BlobField1 | ...
1 | 30 | | | | | | ... |
2 | | | | 21312412414 | | | ... |
Any other ideas?
Thanks for your help!
From what I can see, your second way of attempting this is going to be the easiest way... your queries will be ten fold simpler to handle, and you wouldn't need to programmably create tables on the fly... so my suggestion would be to modify your second idea slightly... Just to clarify something also, A blob will slow down the query speed so I am changing the block to hold the source link to the image instead.
TABLE: Collections
ID| Collection
1 | Poststamps
2 | Barbie Dolls
Table: CollectionData
fkCollection | DataType | VALUE | FieldName |
1 | Double | 30 | StampID |
1 | String | London | StampName |
1 | ImgSrc | ../loc | StampPic |
2 | String | Ken | BarbieName |
2 | ImgSrc | ../loc | BarbiePic |

MySQL select rows where values in one field match values in another table

I have the following tables:
tbljobpost with fields job_id int, job_title varchar(50), skill_cat_id varchar(50)
tblappskills with fields app_control Int, skill_cat_id Varchar(50)
tbljobpost:
+-------------------------------------+
| job_id | job_title | skill_cat_id |
+-------------------------------------+
| 1 | Programmer | 1,2,3,4,5,6,7 |
+-------------------------------------+
tblappskills:
+-----------------------------+
| app_control | skill_cat_id |
+-----------------------------+
| 1 | 1,2,4,5,6 |
| 2 | 1,2,3,7,4 |
| 3 | 1,2,3,4,5,6,7 |
| 4 | 7,1,4,5,6,2,3 |
+-----------------------------+
How can I query or filter the tblappskills that is the same skill_cat_id separated with comma from tbljobpost?
And the result will come like this:
+-----------------------------+
| app_control | skill_cat_id |
+-----------------------------+
| 3 | 1,2,3,4,5,6,7 |
| 4 | 7,1,4,5,6,2,3 |
+-----------------------------+
Your solution does not fit first normal form
You should store skill categories in separate cells like this:
+----------------------------+
| app_control | skill_cat_id |
+----------------------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 4 | 4 |
+----------------------------+
Then, you can easily JOIN the tables and select the rows matching the value you want. It does not matter that app-control-id appears more than once in the table. However, if you decide to put additional data to that table, you should do it in separate table to avoid redundancy, like this:
Your new table (contains detail information about app-control) is related to the table I have mentioned above as 1 : (0..N). It's hard to explain but easy to design. If you study the normal forms mentioned above, you will understand this easily.