MySQL: Updating values of "children" from their "parents" - php

I have a database with rows of "parents" and "children". Similar entries, but one entry is generic version of the more specific child. However, I want these entries to match exactly in certain columns.
Here's an example of my database:
| ID | IsChildOfID | Food | Type |
| 1 | | | Fruit |
| 2 | 1 | Apple | Fruit |
| 3 | 1 | Pear | Vegetable |
| 4 | 1 | Banana | Vegetable |
| 5 | | | Vegetable |
| 6 | 5 | Lettuce | Fruit |
| 7 | 5 | Celery | Vegetable |
| 8 | 5 | Cabbage | Fruit |
In this example there are 2 parents and 6 children. The value of "type" field is inconstant with some of the children. I want to be able to find any children in the database and replace it with their parent's value in only some of the columns. Is this possible with purely MySQL or do I need do it with php? Thanks.

UPDATE name_of_table SET Type = "Fruit" WHERE IsChildOfID = 1
and
UPDATE name_of_table SET Type = "Vegetable" WHERE IsChildOfID = 5
But if you want to do it dynamicaly please use php or some other language...
Also I would prefer to use 2 tables for this kind of data...

Generally, when you use parent/children relationships in sql, you should make two separate database tables for each. In your case, you should create a database entitled "types" and include a type_id for each element in the child table.
Example
Child table:
| ID | TYPE_ID | Food |
| 2 | 1 | Apple |
| 3 | 2 | Pear |
| 4 | 2 | Banana |
| 6 | 1 | Lettuce |
| 7 | 2 | Celery |
| 8 | 1 | Cabbage |
Type table:
| ID | Type |
| 1 | Fruit |
| 2 | Vegetable |
You can then reference it by looping through the type table, and using a sql statement like
$types = mysql_query ( 'SELECT * FROM type_table');
WHILE ( $type = mysql_fetch_array ( $types ) )
{
$sql = 'SELECT * FROM child_table WHERE TYPE_ID = "' . $type['type'] . '"';
}

Similar answer here:
UPDATE multiple tables in MySQL using LEFT JOIN
I was going to write this:
UPDATE foods c
JOIN foods p ON p.id = c.IsChildOfId
SET c.type = p.type
WHERE p.isChildOfId IS NULL
But then upon further reading of the link above, not sure you can reference the target table. Worth a try.

Related

MySQL group rows by one column sorting by another column

I'm developing a PHP script, and I have the following table:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 1 | 1 | 2 | -B-A-C- |
| 2 | 1 | 3 | -B-C- |
| 3 | 2 | 4 | -C-B- |
| 4 | 3 | 1 | -A-B- |
| 5 | 1 | 6 | -A-C- |
| 6 | 2 | 5 | -C-B- |
| 7 | 2 | 7 | -B-C- |
| 8 | 3 | 8 | -A- |
+----+-----------+----------+--------------+
From this table I would like to select all the rows with "feature" LIKE "%-A-%", but displaying first the result with lowest "position", then all the rows that have same value for column "id_parent" of the first result, then row with the 2nd lowest "position" and all the rows that have same "id_parent" of the result with the 2nd lowest "position", and so on...
So the final result should be:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 4 | 3 | 1 | -A-B- |
| 8 | 3 | 8 | -A- |
| 1 | 1 | 2 | -B-A-C- |
| 5 | 1 | 6 | -A-C- |
+----+-----------+----------+--------------+
For some reason I can't explain here I need to have and HAVING clause for selecting the right "feature" value (...HAVING 'feature' LIKE '%-A-%' ...).
Is it possible to make all this with MySQL (possibly without subqueries) or by processing data results with PHP?
Does this help? I've left the last part of the problem as an exercise for the reader...
SELECT a.*
, c.*
FROM my_table a
JOIN
( SELECT id_parent, MIN(position) position FROM my_table WHERE feature = 'a' GROUP BY id_parent ) b
ON b.id_parent = a.id_parent
AND b.position = a.position
JOIN my_table c
ON c.feature = a.feature
AND c.id_parent = a.id_parent;

Laravel 5.1 Eloquent with() and max() using multiple has-many relationships

I'm trying to use Eloquent to find the max value of a column on the last table of a multiple has-many relationship.
Given the following table structure.
Buildings
+----+---------------+
| id | building_name |
+----+---------------+
| 1 | Building 1 |
| 2 | Building 2 |
+----+---------------+
Rooms
+----+-----------+-------------+
| id | room_name | building_id |
+----+-----------+-------------+
| 1 | Room 1 | 1 |
| 2 | Room 2 | 1 |
| 3 | Room 3 | 2 |
+----+-----------+-------------+
maintenancelog
+----+-------------------+---------+---------------------+
| id | maintenance_value | room_id | timestamp |
+----+-------------------+---------+---------------------+
| 1 | Cleaned | 1 | 2015-09-06 00:54:59 |
| 2 | Cleaned | 1 | 2015-09-06 01:55:59 |
| 3 | Cleaned | 2 | 2015-09-06 02:56:59 |
| 4 | Cleaned | 2 | 2015-09-06 03:57:59 |
| 5 | Cleaned | 3 | 2015-09-06 04:58:59 |
| 6 | Cleaned | 3 | 2015-09-06 05:59:59 |
+----+-------------------+---------+---------------------+
I'd like to see if it's possible to generate an eloquent statement that would retrieve the building name, room name, and ONLY the LAST maintenance log date value.
The following works to give me a collection of ALL the values.
$buildings = Building::with('rooms.maintenancelog')->get();
but this errors out, and it looks like it's trying to call max(maintenancelog.timestamp) on the buildings table..
$buildings = Building::with('rooms.maintenancelog')->max('maintenancelog.timestamp')->get();
Error returned:
.....(SQL: select max(`maintenancelog`.`timestamp`) as aggregate from `buildings`)
Am I just asking too much from eloquent, and should I just use the basic query builder
Add the following relationship to the Rooms model...
public function latestMaintenance()
{
return $this->hasOne('App\Maintenancelog')->latest();
}
and change the Eloquent statement to..
$buildings = Building::with('rooms.latestMaintenance')->get();
referenced Getting just the latest value on a joined table with Eloquent

Merge Rows AND/OR Columns into one field in a GROUP BY

My SQL table looks like
+-----+------------+--------------+--------------+-----------+------------+-----------+
| rid | ship_to_id | product_code | product_name | first_row | second_row | third_row |
+-----+------------+--------------+--------------+-----------+------------+-----------+
| 1 | 555 | A | Crystal | 1 | 2 | 3 |
| 1 | 555 | A | Crystal | 4 | 5 | 6 |
| 2 | 333 | B | Diamond | first | second | third |
| 2 | 333 | A | Crystal | ROW 1 | ROW 2 | ROW 3 |
| 2 | 333 | A | Crystal | ROW 4 | ROW 5 | ROW 6 |
+-----+------------+--------------+--------------+-----------+------------+-----------+
And I am trying to get following results
+-----+------------+--------------+-------------------------------------+
| rid | ship_to_id | product_name | data |
+-----+------------+--------------+-------------------------------------+
| 1 | 555 | Crystal | 1 2 3 4 5 6 |
| 2 | 333 | Diamond | first second third |
| 2 | 333 | Crystal | ROW 1 ROW 2 ROW 3 ROW 4 ROW 5 ROW 6 |
+-----+------------+--------------+-------------------------------------+
Can someone please tell me what is wrong with my code. Thanks
$query = mysql_query("SELECT * FROM mytable group by rid, ship_to_id, product_code, product_name");
while($row = mysql_fetch_array($query)) {
echo "$row[rid] $row[ship_to_id] $row[product_code] $row[product_name] $row[first_row] $row[second_row] $row[third_row] <br>";
}
You're forgetting product_code sometimes, I guess.
This probably is bad database design. You should learn about Database normalization and related techniques.
Third, SQL normally produces the same number of columns for every result record, so the best result you could have has rows like 1, 555, "A", "Crystal", "1 4 2 5 3 6" (notice these are 5 values). But I cannot think of a concatenation method that would produce the numbers in the 1 2 3 4 5 6 order. Depending on the better form of your database-schema-to-be, this might be something easier done in PHP than SQL.
If you're still up to concatenating all values into a single cell (as in the newly formatted example output), the you should try something along these lines:
SELECT
rid, ship_to_id, product_code, product_name,
GROUP_CONCAT(CONCAT(first_row,'#',second_row,'#',third_row) SEPARATOR '~') AS data
FROM tablename
GROUP BY rid, ship_to_id, product_code, product_name;
Notice that the values of first_row etc. are separated by a '#' and the values from distinct records by a '~' sign. You can find more options in the documentation of MySQL's GROUP_CONCAT().
you can try something like
select rid,ship_to_id,product_name,
GROUP_CONCAT(CONCAT(first_row,' ',second_row,' ',third_row) as new_val
from mytable
group by rid,ship_to_id,product_name;
As you group by the first 4 columns and use no aggregation function on the remaining ones, data will be lost when multiple record have the same values in the columns grouped by.
So basically the result will be like the following:
+-----+------------+--------------+------------------------------------+
| rid | ship_to_id | product_name | first_row | second_row | thrid_row |
+-----+------------+--------------+------------------------------------+
| 1 | 555 | Crystal | 1 | 2 | 3 |
| 2 | 333 | Diamond | first | second | third |
| 2 | 333 | Crystal | ROW 1 | ROW 2 | ROW 3 |
+-----+------------+--------------+------------------------------------+
That's what group by means. To keep the data of the fields containing varying data, you must use an aggregation function on them, like GROUP_CONCAT. Example:
SELECT rid, ship_to_id, product_code, product_name, GROUP_CONCAT(first_row), GROUP_CONCAT(second_row), GROUP_CONCAT(third_row)
FROM mytable
GROUP BY rid, ship_to_id, product_code, product_name
This will concatenate the values of the corresponding record like this:
+-----+------------+--------------+------------------------------------------+
| rid | ship_to_id | product_name | first_row | second_row | thrid_row |
+-----+------------+--------------+------------------------------------------+
| 1 | 555 | Crystal | 1 4 | 2 5 | 3 6 |
| 2 | 333 | Diamond | first | second | third |
| 2 | 333 | Crystal | ROW 1 ROW 4 | ROW 2 ROW 5 | ROW 3 ROW 6 |
+-----+------------+--------------+------------------------------------------+
However, your approach with GROUP BY is probably not be the best for this case. It would worth to think about to just order by the rid, ship_to_id, product_code, product_name columns, and then iterate through them in PHP with the given order and merge the corresponding rows there.
You may also have to reconsider you database design, as a query like this smells about a bad concept :)

Dynamical column names depending on the single row of the query result

I need some help with dynamical column names in a sql query. First I will try to explain my database structure and then the problem.
Database structure:
admin_group table:
+--------+----------------+
| id | language_code |
+--------+----------------+
| 1 | en_UK |
| 2 | de_DE |
| 3 | es_ES |
+--------+----------------+
constructions_meta table:
+--------+-----------------+----------+
| id | admin_group_FK | value |
+--------+-----------------+----------+
| 1 | 1 | 0.13 |
| 2 | 2 | 0.12 |
| 3 | 3 | 0.10 |
+--------+-----------------+----------+
construction_lang table:
+--------+-----------------+----------------+----------------+
| id | en_UK_name | de_DE_name |es_ES_name |
+--------+-----------------+----------------+----------------+
| 1 | Construction 1 | Konstruktion 1 | Construcción 1 |
| 2 | Construction 2 | Konstruktion 2 | Construcción 2 |
| 3 | Construction 3 | Konstruktion 3 | Construcción 3 |
+--------+-----------------+----------------+----------------+
Those are my tables in the database. What I need here is to get the names of the constructions regarding the language code for each construction. For example I want to list the constructions as following:
Construction 1
Konstruktion 2
Construcción 3
select c.id , (select case c.admin_group_FK
when 1 then l.en_UK_name
when 2 then l.de_DE_name
else l.es_ES_name
end from construction_lang l where id = c.id) as construction_name,
c.value
from constructions_meta c;
Yes the solution is only a get-you-working-quickly one .
And Rob is correct about improving the data model
Solution is :
you need to and drop the construction_lang table from your data model and replace it with this association table much like what Rob proposes
+------------------+--------------+----------------+
| construction_fk | language_fk | name |
+------------------+--------------+----------------+
| 1 | 1 | Construction 1 |
| 1 | 2 | Konstruktion 1 |
| 1 | 3 | Construcción 1 |
| 2 | 1 | Construction 2 |
| 2 | 2 | Konstruktion 2 |
| 2 | 3 | Construcción 2 |
| 3 | 1 | Construction 3 |
| 3 | 2 | Konstruktion 3 |
| 3 | 3 | Construcción 3 |
+------------------+--------------+----------------+
then query you require is
select c.id, l.language_code , a.name ,c.value
from constructions_meta c
join construction_lang_assoc a on a.construction_fk = c.id and a.language_fk = c.admin_group_FK
join admin_group l on l.id = c.admin_group_FK;
It's probably easiest to do that in two passes. That'll give you the ability to expand the width of the construction_lang table without affecting the SQL statements significantly.
Firstly you issue a SQL statement to get the column names that you need, then you use that result set to build a second SQL statement that will get the names for you.
This is not the ideal solution, as it is working around the data-model.
Given a query that returns back the id and language from admin_group in line with:
array( array( 'id' => 1, 'language_code' => 'en_UK' ),
array( 'id' => 2, 'language_code' => 'de_DE' ),
array( 'id' => 3, 'language_code' => 'es_ES' ) )
You can build a statement with something along the lines of (using the other answer from diarmuid as an example)
$sql = "select c.id , (select case c.admin_group_FK\r\n";
foreach( $languages as $thisLanguage ) {
$sql .= "when {$thisLanguage['id']} then l.${thisLanguage['language_code']}_name\r\n";
}
$sql .= ...
This is a round-about way of doing it, because of the data-model you have. Ideally you wouldn't need the "dynamic" SQL."
If you want to remove the round-about-ness, and build something more in line with standard relational database theory, then you can change the model so that the construction_lang table is more like this:
+------------------------+-----------------+----------------+
| constructions_meta_fk | language | name |
+------------------------+-----------------+----------------+
| 1 | en_UK | Construction 1 |
| 1 | de_DE | Konstruktion 1 |
| 1 | es_ES | Construcción 1 |
| 2 | en_UK | Construction 2 |
| 2 | de_DE | Konstruktion 2 |
| 2 | es_ES | Construcción 2 |
| 3 | en_UK | Construction 3 |
| 3 | de_DE | Konstruktion 3 |
| 3 | es_ES | Construcción 3 |
+------------------------+-----------------+----------------+
you can use if condtion like `
if(admin_group_FK == 1)
{
fetch['en_UK_name'];
}
if(admin_group_FK == 2)
{
fetch['de_DE_name'];
}
if(admin_group_FK == 3)
{
fetch['es_ES_name'];
}
`

Mysql query to reindex items

I have a table as follows, it has an ID and item name,order and type.
+--------+----------------+-----------+---------+
| ID | ITEM | ORDER | TYPE |
+--------+----------------+-----------+---------+
| 1 | Banana | 2 | Fruit |
+--------+----------------+-----------+---------+
| 2 | Apple | 1 | Fruit |
+--------+----------------+-----------+---------+
| 3 | Orange | 4 | Fruit |
+--------+----------------+-----------+---------+
| 4 | Lemon | 3 | Fruit |
+--------+----------------+-----------+---------+
If I were to delete say Lemon the ORDER would be 1,2,4. Is there a method to reindex the remaining items to get the following result?
+--------+----------------+-----------+---------+
| ID | ITEM | ORDER | TYPE |
+--------+----------------+-----------+---------+
| 1 | Banana | 2 | Fruit |
+--------+----------------+-----------+---------+
| 2 | Apple | 1 | Fruit |
+--------+----------------+-----------+---------+
| 3 | Orange | 3 | Fruit |
+--------+----------------+-----------+---------+
Right before you delete the Lemon row - check what its ORDER column value is.
After that perform
UPDATE tbl SET `ORDER` = `ORDER` - 1 WHERE `ORDER` > value_lemon_had
There's no way to do this completely automatically, but it would be fairly simple to do. Assuming that ORDER is not unique it's easier, but if it is you would have to retrieve the ORDER of "Lemon" first. Then, just do:
UPDATE Fruit SET ORDER = ORDER - 1 WHERE ORDER > ?
Where ? is Lemon's ORDER.
Assuming ORDER is not unique, then you could just do this query beforehand, but you would join on Fruit selecting Lemon's order to replace ?.

Categories