Hey guys...I am trying to get some data based on a condition which I don't know how to write the code for.
My table looks like this:
id | meta_id | key | value
-- ------- --- -----
1 | 1 | image | 0
2 | 1 | path | 1
3 | 1 | location | parent
4 | 2 | image | 1
5 | 2 | path | 2
6 | 2 | location | self
From this how can i return the meta_id that has key = image and value = 1 and value = 2..so you see i have value 2 times...can this be done?
I hope I've understood
select * from table where key = 'image' and value in (1,2)
Related
Got big table (~1.6m rows), which looks like:
------------------------------------
| id | text | image_id | order |
------------------------------------
| 2 | random | 12 | 1 |
------------------------------------
| 3 | random | 12 | 2 |
------------------------------------
| 5 | random | 12 | 1 |
------------------------------------
| 6 | random | 12 | 2 |
------------------------------------
| 8 | random | 17 | 1 |
------------------------------------
| 9 | random | 17 | 1 |
------------------------------------
The goal is to have:
------------------------------------
| id | text | image_id | order |
------------------------------------
| 2 | random | 12 | 1 |
------------------------------------
| 3 | random | 12 | 2 |
------------------------------------
| 8 | random | 17 | 1 |
------------------------------------
Many rows with different ids but other data is the same, so we need to keep only one row of each order number (111222333444 need to be 1234).
This query is working for small tables:
DELETE n1 FROM table n1, table n2 WHERE image_id = 12 AND n1.id > n2.id AND n1.order = n2.order
But for big tables query takes too long, so receive timeout.
DB Backend is Laravel & PHP, and we use chunks to query this tables.
Basically goal is to crawl through big table based on image_id and remove duplicates so for each image_id we got rows with order columns like: 1,3,4,5,6,7,8 etc.
Just use unique() method from laravel
$uniqueData = Model::unique(function ($item) {
return $item['image_id'].$item['order'];
});
$uniqueData->values()->all();
return $uniqueData;
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
I have a table with images in SQL database.
It looks something like this.
SELECT * FROM eshop_images WHERE fg_idProduct='$id' AND main='1'
+----------+-----+------+--------------+
| idImages | url | main | fg_idProduct |
+----------+-----+------+--------------+
| 1 | x | 0 | 1 |
| 2 | x | 1 | 1 |
| 3 | x | 0 | 1 |
| 4 | x | 0 | 2 |
| 5 | x | 0 | 2 |
| 6 | x | 0 | 2 |
| 7 | x | 1 | 2 |
+----------+-----+------+--------------+
Each product can only have one main image, but what if I want to set some other image which is 0 as main one?
I have to go through the whole table where fg_idProduct=$id and SET them to null. I don't know how to do this, can somebody help me?
Thanks.
Try this:
UPDATE eshop_images
SET main = 0
WHERE main = 1
AND fg_idProduct = $id
UPDATE eshop_images
SET main = 0
WHERE main = 1
AND fg_idProduct = $id
I posted another question that is similar.
So still having more problems.
This is the case, I want to be able to update the column position.
following the example below. here is before the update
+----+--------------+----------+
| id | question | position |
+----+--------------+----------+
| 1 | Question 1 | 1 |
| 2 | Question 2 | 2 |
| 3 | Question 3 | 3 |
| 4 | Question 4 | 4 |
+----+--------------+----------+
So If I update the row id = 2 the column position from 2 to 4, I want to be organized as the following. here is after the update
+----+--------------+----------+
| id | question | position |
+----+--------------+----------+
| 1 | Question 1 | 1 |
| 2 | Question 2 | 4 |
| 3 | Question 3 | 2 |
| 4 | Question 4 | 3 |
+----+--------------+----------+
I tried many thing and nothing works.
So if anyone could help me will be very apprecciated.
Thanks a lot.
You want to do something like this:
update t join
(select position from t where id = 2) t1
on t.position >= t1.position
set position = (case when id = 2 then 4 else position - 1 end)
That is, subtract "1" from all positions greater than the one with id = 2. And set that position to "4".
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.