Manipulating Foreign Keys output in MYSQL - php

I have a table connected to another table through foreign key;
First Table is the brand; it has a brand_id and brand_name fields
Second Table is the Product Line; it has a Line_id and line_name fields
Third Table is the Lines Offered; it has a id, Brand (which is a foreign key from the first table), and Line_name (which is a foreign key from the second table.
If i will look into the third table on mysql the fields contains the id numbers of the foreign keys.
My question is this, is it possible that the stored value will be the name itself and not the ID? or Is it possible to add a field on my third table named it as Brand_name which is a VARCHAR that will display the exact brand name from the first table. Example the values of my third table would be '1','3','The Brand Name of brand with id no. 3','25'; - If yes i dont have any idea how to do this.

Nothing is preventing you from doing this. You would simply add a brand_name field to the "third table"... The question begging to be asked is: why do you want to do this?
brands
_________________________
| brand_id | brand_name |
| 1 | brand1 |
| 2 | brand2 |
| 3 | brand3 |
| 4 | brand4 |
| 5 | brand5 |
-------------------------
lines
_________________________
| line_id | line_name |
| 1 | line1 |
| 2 | line2 |
| 3 | line3 |
| 4 | line4 |
| 5 | line5 |
-------------------------
linked
_________________________________________________
| id | line_id | brand_id | brand_name |
| 1 | 5 | 1 | brand1 |
| 2 | 5 | 2 | brand2 |
| 3 | 4 | 2 | brand2 |
| 4 | 4 | 3 | brand3 |
| 5 | 4 | 3 | brand3 |
| 6 | 3 | 4 | brand4 |
| 7 | 3 | 4 | brand4 |
| 8 | 2 | 4 | brand4 |
| 9 | 2 | 5 | brand5 |
| 10 | 1 | 5 | brand5 |
------------------------------------------------
That's your proposed setup. Now if we:
SELECT brand_id, brand_name FROM linked WHERE line_id = 4;
We would get:
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
But the same could be achieved without duplicate data (in large databases it's pretty unreasonable to have duplicate data like that), and without needing to update BOTH the linked and brand tables every time the brand name changes by using:
SELECT linked.brand_id, brands.brand_name
FROM brands, linked
WHERE linked.line_id = 4 AND brands.brand_id = linked.brand_id;
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
That answer didn't need to be that long. I was having fun making tables.

Related

How to update one table based on another one?

I have these two tables:
// user
+----+-------+------------+
| id | name | total_rep |
+----+-------+------------+
| 1 | Jack | 100 |
| 2 | Peter | 334 |
| 3 | John | 1 |
| 4 | Ali | 5463 |
+----+-------+------------+
// rep
+----+------------+---------+------+
| id | reputation | id_user | done |
+----+------------+---------+------+
| 1 | 5 | 2 | 1 |
| 2 | 2 | 3 | 1 |
| 3 | 15 | 2 | Null |
| 4 | 10 | 2 | Null |
| 5 | 5 | 4 | 1 |
| 6 | 10 | 3 | Null |
+----+------------+---------+------+
I'm trying to sum the number of reputation column from rep table where done is Null for specific user and then add it to total_rep column from user table. So it is expected output:
// specific user
$id = 2;
// user
+----+-------+------------+
| id | name | total_rep |
+----+-------+------------+
| 1 | Jack | 100 |
| 2 | Peter | 359 | -- this is updated
| 3 | John | 1 |
| 4 | Ali | 5463 |
+----+-------+------------+
Note: Then I will update done column and set all Null values for that user to 1. (this is not my question, I can do that myself)
How can I do that?
One way to do it is to use the result of a subquery as a scalar value in an update statement.
UPDATE `user`
SET total_rep = total_rep + (
SELECT SUM(reputation) AS rep_sum FROM `rep` WHERE done IS NULL AND id_user = 2)
WHERE id = 2;
You can't update two tables in one statement, so you will need to execute a transaction. Or maybe in some code do the next thing I'll make it in PHP
$query_result=DB::select('Select sum(reputation) as reputation, id_user from rep where done is null group by id_user');
foreach($query_result as $result){
//update the data
}
You have to take the data and update firstable the reputation table to clean it and then the user table to sum the rep values

PHP and MYSQL select with 5 tables and multiple rows as a single array

Would like to get the following as a result from the table structure below (MYSQL + PHP)
array[0][name]1,[desc]red,[title]hero,[desc]strong,[desc2]smells,[img][0]red1,[img][1]red2,[img][2]red3,ext[0].jpg,[ext][1].gif,[ext][2].png,[count][0]253,[count][1]211,[count][2]21,[count][3]121,[dist][0]5,[dist][1]5,[dist][2]12,[dist][3]2,[score][0]2,[score][1]3,[score][2]1,[score][3]5,[score][4]4,[val][0]5,[val][1]1,[val][2]4,[val][3]3,[val][4]4
The problem I have with a simple SELECT, JOIN and GROUP_CONCAT is that the values duplicate after selecting all the images.
I've tried various other ways for example selecting the data by row combined with a foreach loop in PHP, but I end up with lots of duplicates, and it looks very messy.
I also though about splitting it into multiple selects instead of using one, but I really would like to know if it can be done with one select.
Could someone help me with an MYSQL select? Thanks
game
+-----+----------+
| pid | name |
+-----+----------+
| 1 | red |
| 2 | green |
| 3 | blue |
+-----+----------+
detail
+-----+------+--------+-------+--------+
| id | pid | title | desc | desc 2 |
+-----+------+--------+-------+--------+
| 1 | 1 | hero |strong | smells |
| 2 | 2 | prince |nice | tall |
| 3 | 3 | dragon |big | green |
+-----+------+--------+-------+--------+
image
+-----+-----+-----+----+
| id | pid | img |ext |
+-----+-----+-----+----+
| 1 | 1 | red1|.jpg|
| 2 | 1 | red2|.gif|
| 3 | 1 | red3|.png|
+-----+-----+-----+----+
devmap
+-----+-----+-------+------+
| id | pid | count | dist |
+-----+-----+-------+------+
| 1 | 1 | 253 | 5 |
| 2 | 1 | 211 | 5 |
| 3 | 1 | 21 | 12 |
| 4 | 1 | 121 | 2 |
+-----+-----+-------+------+
stats
+-----+-----+-------+------+
| id | pid | scrore| val |
+-----+-----+-------+------+
| 1 | 1 | 2 | 5 |
| 2 | 1 | 3 | 1 |
| 3 | 1 | 1 | 4 |
| 4 | 1 | 5 | 3 |
| 5 | 1 | 4 | 3 |
+-----+-----+-------+------+
When you do a JOIN that involves more than a 1:1 mapping between tables you're going to have duplicate data, and there's no way to get around that in the query.
You can break it out into multiple selects, or you can loop through the result set and pare out whatever duplicate information you don't want.

PHP&MySQL: Combine more lines and put it in array [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
mysql with comma separated values
I have two tables with more lines, like this:
1. numberstable
---------------------------
| number_id | number_name |
---------------------------
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
.
.
.
---------------------------
2. testtable
-------------------------------
| test_id | numbers | sthelse |
-------------------------------
| 1 | 2.4.5.6 | text1 |
| 2 | 4.8.7.1 | text2 |
| 3 | 2.7.8.5 | text3 |
-------------------------------
First I would like to combine all three "numbers" rows from table "testtable" to get something like this: 1.2.4.5.6.7.8 and then exclude it in next query. This query is "SELECT number_id, number_name FROM numberstable ORDER BY number_name". After excluding I would like to show just numbers which aren't in use in "testtable" (9, 10, 11, ...).
How to do that?
If you are trying to relate the numberstable to the testable. I would think you would be much better served to add another table that would relate the two to where you had a schema like
1. numberstable
---------------------------
| number_id | number_name |
---------------------------
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
.
.
.
---------------------------
2. testtable
---------------------
| test_id | sthelse |
---------------------
| 1 | text1 |
| 2 | text2 |
| 3 | text3 |
--------------------
3. numbersintesttable
-----------------------
| test_id | number_id |
-----------------------
| 1 | 2 |
| 1 | 4 |
| 1 | 5 |
| 1 | 6 |
| 2 | 4 |
| 2 | 8 |
| 2 | 7 |
| 2 | 1 |
-----------------------
So the new table would be a many-to-many join table that you could use to get all your needed data in a single query by utilizing the type of joins you want (INNER, OUTER, etc.)

Add dynamic columns to MySQL result

I have a table in mysql, let's call it foo and it has a limitied amount of columns.
| id | name | date |
--------------------------
| 1 | bar | 2012-05-08 |
| 2 | buba | 2012-05-09 |
My users can add records to the table foo_field (stuff like, code, description, time...).
| id | name |
--------------------
| 1 | code |
| 2 | description |
| 3 | time |
In the table foo_field_value the values for the user-defined fields are stored, like so:
| id | foo_id | foo_field_id | value |
------------------------------------------
| 1 | 1 | 1 | b |
| 2 | 1 | 2 | Lalalala |
| 3 | 1 | 3 | 12:00 |
| 1 | 2 | 1 | c |
| 2 | 2 | 2 | We are go |
| 3 | 2 | 3 | 14:00 |
Ideally, I'd want one query which would give me a result like
| id | name | date | code | description | time |
------------------------------------------------------
| 1 | bar | 2012-05-08 | b | Lalalala | 12:00 |
| 2 | buba | 2012-05-09 | c | We are go | 14:00 |
Is this even possible without doing an inner join on the foo_fields_value table for every foo_field (generating the query with PHP by doing another query first).
It's possible to do it in just one, and quite simple.
We are going to modify the foo_field table a bit, adding a column corresponding to the foo table's id column, which I assume is the primary key.
so now we have
* foo
|------|
| id |
| name |
| date |
|------|
* foo_field
|-------------|
| foo_id |
| code |
| description |
| time |
|-------------|
Which means we can add the extra fields with one simple query:
SELECT * FROM foo
LEFT JOIN foo_field ON foo.id = foo_field.foo_id
Which will give us a result set of
| id | name | date | foo_id | code | description | time |
|----+-------+------------+--------+--------+-------------+----------|
| 1 | asdw | 2012-05-16 | 1 | asdasd | asdasd | 15:03:41 |
| 2 | fdgfe | 2012-05-18 | 2 | asdas | asdas | 15:03:41 |
| 3 | asdw | 2012-05-16 | 3 | asdas | asdas | 15:03:52 |
| 4 | fdgfe | 2012-05-18 | 4 | asdasd | asdasd | 15:03:52 |
I am still not sure I surely understood your question. If you want to create truly dynamic values and datastructures, I suggest you save a serialized array into a TEXT field in your database, but I also suggest you to overlook your solution if this is the case; if you want your solution to be able to grow, you want to manage as strict structures as possible.
What you are looking for is a pivot query. And since you have dynamic fields that need to converted to columns, check this article here about making automatic pivot queries
http://www.artfulsoftware.com/infotree/queries.php#523

select data from database Mysql query

tbl_pack_service;
+-------+---------+------------+
| ps_id | pack_id | service_id |
+-------+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 2 | 4 |
| 7 | 2 | 5 |
| 8 | 3 | 1 |
| 9 | 3 | 2 |
| 10 | 3 | 3 |
| 11 | 3 | 4 |
+-------+---------+------------+
ps_id is primary key
i am tying to make dynamic select list in php
i want a mysql query that can give service id which do not match with particular pack_id
like i have service_id 1,2,3,4,5
when i should select pack_id=1 then 3,4,5 should be displayed
and when i should select pack_id=2 then nothing should be displayed as it has all the 5 services.
thanks..
There are a few ways to handle this. The easiest is with a NOT IN subquery:
SELECT DISTINCT service_id
FROM
tbl_pack_service
WHERE service_id NOT IN (SELECT service_id FROM tbl_pack_service WHERE pack_id = 1)

Categories