I want to know if i'm setting up this database schema the most effective way.
I am trying to output snowboarding terminology and what it means for each product on my website. The tables that I have set up for this:
products
+----+--------+--------+-------------+
| id | brand | name | product_type|
+----+--------+--------+-------------+
|8000| burton | diode | binding |
+----+--------+--------+-------------+
terminology_products
+----+--------+------------+
| id | att_id | product_id |
+----+--------+------------+
| 1 | 51 | 8000 |
| 2 | 52 | 8000 |
+----+--------+------------+
terminology
+--------+-----------+--------+---------------------+
| att_id | type | key | value |
+--------+-----------+--------+---------------------+
| 52 | baseplate | EST | details about EST |
| 53 | baseplate | Hinge | details about Hinge |
+--------+-----------+--------+---------------------+
Then I query
SELECT products.ProductName, products.Brand, terminology.type, terminology.key, terminology.value
FROM products
JOIN terminology_products
ON terminology_products.product_id = products.ID
JOIN terminology
ON terminology_products.att_id = terminology.att_id
WHERE products.id = 8000
And get the results that I am looking for, but I feel like this could be an over complicated way of doing it. Is there a simpler way? Thank you for any insight or help.
Yes, it is a correct way. It's common solution.
Your "terminology_products" table contains foreign keys, and it allows you to make "many to many" relation between tables.
Related
I have 2 tables. One with a list of clients and another with a list of data. I am trying to create a table in my view that lists the client name along with the sum of a column(job_total) in the data table. I am able to write a query that works fine in most situations. The problem is, if I have not yet created a record in the data table I need to still display the client name with a balance of zero on my table in my view. Need some direction on how to handle this. I was thinking I need to query my list of clients and loop through that query just not sure how to do it.
I want my view to look like below:
+-------------+---------+
| Client Name | Balance |
+-------------+---------+
| xxx | $75.00 |
| xxx | $100.00 |
| xxx | $0.00 |
+-------------+---------+
Here is a rough layout of the two tables in my database:
cj_clients
+----+-------------+
| id | client name |
+----+-------------+
| 1 | client1 |
| 2 | client2 |
| x | xxx |
+----+-------------+
cj_data
+----+-----------+-----------+
| id | client_id | job_total |
+----+-----------+-----------+
| 1 | 1 | 5.00 |
| 2 | 1 | 10.00 |
| 3 | 1 | 15.00 |
+----+-----------+-----------+
The below code returns the desired results except when no entries have yet been made to the cj_data table. Not sure how to still get the client in the table view with a balance of $0.
$this->db->select('client_name,client_id, sum(job_total) AS balance')
->from('cj_data')
->join('cj_clients','cj_data.client_id = cj_clients.id')
->group_by('client_name');
return $this->db->get()->result();
You need to give left join
$this->db->select('client_name,client_id, IFNULL(sum(job_total),0) AS balance')
->from('cj_data')
->join('cj_clients','cj_data.client_id = cj_clients.id',"left") // here
->group_by('client_name');
return $this->db->get()->result();
I wrote IFNULL condition if record not found or it will show all data for all clients in cj_clients
Note: the Default behaviour of CodeIgniter is it will add inner join
if join not specified
Hello :) I am fairly new to using INNER JOIN and still trying to comprehend it's logic which I think I am sort of beginning to understand. After being across a few different articles on the topic I have generated a query for finding duplicates in my table of phone numbers.
My table structure is as such:
+---------+-------+
| PhoneID | Phone |
+---------+-------+
Very simple. I created this query:
SELECT A.PhoneID, B.PhoneID FROM T_Phone A
INNER JOIN T_Phone B
ON A.Phone = B.Phone AND A.PhoneID < B.PhoneID
Which returns the ID of a phone that matches another one. I don't know how to word that properly so here is an example output:
+---------+---------+
| PhoneID | PhoneID |
+---------+---------+
| 17919 | 17969 |
| 17919 | 22206 |
| 17919 | 23837 |
| 17920 | 17970 |
| 17920 | 22203 |
| 17920 | 23834 |
| 17921 | 17971 |
| 17921 | 22225 |
| 17921 | 22465 |
| 17921 | 24011 |
| 17921 | 24047 |
| 17922 | 17972 |
| 17922 | 22198 |
| 17922 | 23879 |
| 17923 | 17973 |
| 17923 | 22199 |
| 17923 | 23880 |
+---------+---------+
You can note that on the left there is repeating IDs, the phone number that matches will be on the right (These are just the IDs of said numbers). what I am trying to accomplish, is to actually change a join table relative to the ID on the right. The join table structure is as such:
+----------+-----------+
| T_JoinID | T_PhoneID |
+----------+-----------+
Where T_JoinID is a larger object with a collection of those T_PhoneIDs, hence the join table. What I want to do is take a row from the original match query, and find the right side PhoneID in the join table, then update that item in the Join to be equal to the left side PhoneID. Repeating this for each row.
It's sort of a way to save space and get rid of matching numbers, I can just point the matching ones to the original and use that as a reference when I need to retrieve it.
After that I need to actually delete the original numbers that I reset the reference for but... This seems like a job for 2 or 3 different queries.
EDIT:
Sorry I know I didn't include enough detail. Here is some additional info:
My exact table structure is not the same as here but I am only using the columns that I listed so I didn't consider the fact that any of the others would matter. Most of the tables have a unique ID that is auto incremented. The phone table has carrier, type, ect columns. The additional columns I felt were irrelevant to include, but if there is a solution that includes the auto incremented ID of each table, let me know :) Anyway, I sort of found a solution, using multiple queries though I am still interested to learn and apply knowledge based on this question. So I have a that join table that I mentioned. It might look something like this for the expected results. There is a before and after table in one sorry for poor formatting.
+--------------------+---------+----------+---------+
| Join Table Results | | | |
+--------------------+---------+----------+---------+
| Before | | After | |
| Join | Table | Join | Table |
| PersonID | PhoneID | PersonID | PhoneID |
| 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 2 |
| 1 | 3 | 1 | 3 |
| 2 | 4 | 2 | 1 |
| 2 | 5 | 2 | 5 |
| 2 | 6 | 2 | 6 |
| 3 | 7 | 3 | 5 |
| 3 | 8 | 3 | 5 |
| 3 | 9 | 3 | 5 |
| 3 | 10 | 3 | 8 |
| 3 | 11 | 3 | 9 |
+--------------------+---------+----------+---------+
So you can see that in the before columns, 7, 8, and 9 would all be duplicate phone numbers in the PhoneID - PhoneID relationship table I posted originally. After the query I wanted to retrieve the duplicates using the PhoneID - PhoneID comparison and take the ones that match, to change the join table in a way that I have shown directly above. So 7, 8, 9 all turn to 5. Because 5 is the original number, and 7, 8, 9 coincidentally were duplicates of 5. So I am basically pointing all of them to 5, and then deleting what would have been 7, 8, 9 in my Phone table since they all have a new relationship to 5. Is this making sense? xD It sounds outrageous typing it out.
End Edit
How can I improve my query to accomplish this task? Is it possible using an UPDATE statement? I was also considering just looping through this output and updating each row individually but I had a hope to just use a single query to save time and code. Typing it out makes me feel a tad obnoxious but I had hope there was a solution out there!
Thank you to anyone in advance for taking your time to help me out :) I really appreciate it. If it sounds outlandish, let me know I will just use multiple queries.
Suppose I a table like this:
Categories:
| cat_id | cat_name |
|--------|----------|
| 1 | tvs |
| 2 | phones |
| 3 | tablets |
And then I have:
Products:
| product_id | product_name | product_category |
|------------|-----------------|------------------|
| 1 | tv sony | 1 |
| 2 | tv samsung | 1 |
| 3 | phone htc | 2 |
| 4 | phone motorolla | 2 |
| 5 | tablet apple | 3 |
And someone goes into index.php where I get 10 random products from the DB. For creating the link I'd have to fetch the database again (to get the name of the category) and then I could do something like mysite.com/phones/4 (that would be the link to phone motorolla).
Of course 10 wouldn't be too hard on the server but it's still another fetch on the database.
The other option is to fetch for the category name during the product creation, and generate a link there, something like:
| product_id | product_name | product_category | product_link |
|------------|-----------------|------------------|--------------|
| 1 | tv sony | 1 | tvs/1 |
| 2 | tv samsung | 1 | tvs/2 |
| 3 | phone htc | 2 | phones/3 |
| 4 | phone motorolla | 2 | phones/4 |
| 5 | tablet apple | 3 | tablets/5 |
However the name of the category may change and so the link wouldn't work anymore. So what should I do? Is there another way to do this?
Alright Nick so in SQL you have something very powerful called JOIN. A join in SQL allows you to merge columns from different tables based on a key. Usually the key is a primary key or a foreign key.
In your case, it seems you have as primary keys : cat_id and product_id
And as foreign key you have : product_category
Based on your database schema, you can query your tables like this :
SELECT p.product_id, p.product_name, c.cat_name
FROM PRODUCTS p INNER JOIN CATEGORIES c
ON p.product_category = c.cat_id
With this query you retrieve your product_id, product_name and cat_name based with only one query for your database.
Here is more info about joins in SQL => https://www.w3schools.com/sql/sql_join.asp
Hope it will help you.
I have 14 tables (one for every year) with product code, firm name and invoice numbers. Main structure of table is identical (product code, ID), but there can be some variables in names of firms.
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
Column Firm1 do not usually equals to same firm as firm 1 in other table
I am using table editor to work with tables (adding columns to table, editing values…).
I would like to know if it is possible to achieve result like below. It is above my PHP skills.
Product G-00001 page
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
Lemme begin with book recommendation : SQL Antipatterns. You will need it, doesn't matter if you caused this mess or ar just assigned to fix it.
If i was in your place, first thing would do would be to fix the database structure. This is madness. You do not need a new table for each year and new column for each company. Database is not a form of Excel spreadsheet.
Invoices Years Companies
----------------- ------------- ---------------
| product_code PK | | year_id PK | | company_id PK |
| company_id FK | | number | | title |
| amount | ------------- ---------------
| year_id FK |
-----------------
Where PK - primary key and FK - foreign key.
This structure would make the gathering of information much much much MUCH easier.
If you just want to display the data and not worry about the restructuring just yet you can use a JOIN to display the information from all the tables.
Although I would agree with teresko you really need to redesign that database. It is not maintainable the way it is.
I'm doing a mysql query where I want to join two tables (Projects and Country):
+----------------+ +-------------------------+
| Project ID | | id |
+----------------+ +-------------------------+
| Name | | name |
+----------------+ +-------------------------+
| countryid | | lat, long (two columns) |
+----------------+ +-------------------------+
I'm using this query:
SELECT country.name, country.latitude, country.longitude
FROM projects JOIN country ON projects.countryid = country.id;
Fine until now! What happens next is each project has a class (TO-DO, DOING, DONE). Each class is one table that has all the project ids that belong to each.
The structure has to be like this, because each class has additional information. And that information only exists if the project belongs to that class. For example, a project in the TO-DO class has a "start_prevision_date" which is only valid in the TO-DO table (fields from other classes are still null).
What I want to do is assign one color to each project depending if he is TO-DO, DOING or DONE. A perfect solution would be a query that return values like:
ResultSet:
+--------------+------------------+-------------------+-------+-------+------+
| country.name | country.latitude | country.longitude | TO-DO | DOING | DONE |
+--------------+------------------+-------------------+-------+-------+------+
| Portugal | 13,4 | 15,6 | 1 | 0 | 0 |
+--------------+------------------+-------------------+-------+-------+------+
Can be in more than one column at once
Where 1 means he belongs and 0 means he doesn't belong. I could just check, and print the right color!
Is there a way to do this efficiently? Avoiding the use of extra three queries.
I hope I was clear.
You can use a query like this:
SELECT
country.name,
country.latitude,
country.longitude,
IF (TO-DO.sureField IS NULL, 0, 1) as TO-DO,
IF (DOING.sureField IS NULL, 0, 1) as DOING,
IF (DONE.sureField IS NULL, 0, 1) as DONE
FROM projects
LEFT JOIN country ON projects.countryid = country.id
LEFT JOIN TO-DO ON projects.projectid = TO-DO.projectid
LEFT JOIN DOING ON projects.projectid = DOING.projectid
LEFT JOIN DONE ON projects.projectid = DONE.projectid;
sureField has to be a field in each class table that is surely populated and not null when a project is in that class and null otherwise.
I tested in MySql reproducing a similar case and it worked.
This will give you:
ResultSet:
+--------------+------------------+-------------------+-------+-------+------+
| country.name | country.latitude | country.longitude | TO-DO | DOING | DONE |
+--------------+------------------+-------------------+-------+-------+------+
| Portugal | 13,4 | 15,6 | 1 | 1 | 0 |
+--------------+------------------+-------------------+-------+-------+------+
If the projects in DOING remain also in TO-DO, or:
ResultSet:
+--------------+------------------+-------------------+-------+-------+------+
| country.name | country.latitude | country.longitude | TO-DO | DOING | DONE |
+--------------+------------------+-------------------+-------+-------+------+
| Portugal | 13,4 | 15,6 | 0 | 1 | 0 |
+--------------+------------------+-------------------+-------+-------+------+
If the projects in DOING are deleted from TO-DO.
Therefore you will have to manage the query on result set appropriately.
Regards