Pre-Information
I've table called Test:
-- Table Creation
CREATE TABLE Test(
id integer,
title varchar (100)
);
-- Insertion
INSERT INTO Test Values
(1, "Hi"),
(2, 'Hello'),
(2, "Hellew"),
(3, "World"),
(3,"Wordy");
Test Table
| Id | title |
|----|--------|
| 1 | Hi |
| 2 | Hello |
| 2 | Hellew |
| 3 | World |
| 3 | Wordy |
Process
I want to delete the duplicates id
Based on the priority
The Problem
This is the output Error I get
ERROR 1093 (HY000) at line 5: You can't specify target table 'Test' for update in FROM clause
Required OUTPUT
| Id | title |
|----|--------|
| 1 | Hi |
| 2 | Hello |
| 3 | World |
Thanks,
You have not clearly mentioned what is the "priority" here. But looking at the output example, I am assuming that the priority is to keep those strings which are greater than the others with similar id. Here is my code:
delete a.* from Test a join Test b
on a.id = b.id where a.title < b.title;
select * from Test;
Reference
MySQL: ALIASES
Related
So here is my problem, how can i do this :
user with ID = 7 from table A with 3 columns want to insert data to table B with 4 columns but with the same id.
Table A :
| Id | name | password |
| 7 | john | password |
| 9 | mark | password |
| 12 | yuta | password |
Table B :
| Id | user_id | food | drink |
| 1 | 7 | oats | milk |
| 2 | 9 | fish | water |
| 3 | 12 | pear | fanta |
How can i achieve table b in 1 query? both id in both table are primary keys and im using mysql
here's the code i was trying to do :
INSERT INTO table_b SET food = :food, drink = :drink, ( user_id) SELECT a.id FROM table_a u WHERE EXISTS (SELECT * WHERE name = :name AND password = :password)
i know the query is wrong but thats the closest i can do. pls help thank you
You should just use a unique code to link them together.
you can have the code stored in a variable like this
$unique = time().rand(1000, 9999);
//You should have something like this 16659154332358
Create a column that will recieve this value in your database.
Now they have a relationship
i am new to this and want some help.i have table with name "abc" with following entries
+------+--------+------+
| Id | Name | City |
+------+--------+------+
| 101 | john | abc |
| 102 | Miller | cbz |
+------+--------+------+
and another table "xyz"
+------+--------+------+
| Id | Name | City |
+------+--------+------+
| 102 | Miller | cbz |
+------+--------+------+
i applied trigger on table "abc" which will update the table "xyz" with recently inserted values and will delete all previous entries...
for example, when i fire insert query on "abc" table i get ,"abc" as follow
insert into abc Values(103,'Joseph','xyz');
i get output for table "abc" as,
+------+--------+------+
| Id | Name | City |
+------+--------+------+
| 101 | john | abc |
| 102 | Miller | cbz |
| 103 | Joseph | xyz |
+------+--------+------+
and table "xyz" as,
+------+--------+------+
| Id | Name | City |
+------+--------+------+
| 103 | Joseph | xyz |
+------+--------+------+
now my question is how to acheive this using only one table(i dont want to use two table as this is not my requirement).
like following ..
insert into xyz values(104,'Ridhit','pqr');
+------+--------+------+
| Id | Name | City |
+------+--------+------+
| 104 | Ridhit | pqr |
+------+--------+------+
Please help.Trigger i used is
DELIMITER !!
create trigger OnlyOne BEFORE INSERT on abc
for each row
BEGIN
DECLARE a1 INT;
Select count(1) INTO a1 from xyz;
IF a1>0 THEN
delete from xyz limit 1;
insert into xyz(Id,Name,City) values (new.Id,new.Name,new.City);
ELSE
insert into xyz(Id,Name,City) values (new.Id,new.Name,new.City);
END IF;
END;
!!
DELIMITER ;
Do this on application level, but you better make sure you use transactions. You probably don't want to have an empty table, when the operation crashes in the middle.
You may also consider to have following approach.
Instead of deleting/updating whenever an insert occurs, add a column like created with default value of current timestamp to your table so that it looks something like this:
CREATE TABLE abc(
id int auto_increment primary key,
name varchar(50),
city varchar(50),
created timestamp default current_timestamp
);
To get the latest entry you just do
SELECT * FROM abc ORDER BY created DESC LIMIT 1;
Or you put this in a view
CREATE VIEW just_latest_entry_from_abc AS
SELECT * FROM abc ORDER BY created DESC LIMIT 1;
Then you just do
SELECT * FROM just_latest_entry_from_abc;
When table size matters, create a cronjob or a scheduled event to delete older entries on a regular basis.
I am experiencing a problem in MYSQL insert.
Could any experts will answer this.
Below is my question.
Photo table :
pid - PK,
photo_src,
photo_size,
created ,
Photo_link table : - for linking more than one image in Blog table .
id ,
pid -FK
Blog Table :
bid - PK,
content,
created,
id - will have photo_link table id .
Photo :
| pid | photo_src | photo_size | created |
+-----+--------------+------------+---------------------+
| 1 | /photo/1.jpg | 400 | 2014-05-27 11:58:45 |
| 2 | /photo/2.jpg | 400 | 2014-05-27 11:58:54 |
| 3 | /photo/3.jpg | 400 | 2014-05-27 11:59:07 |
| 4 | /photo/4.jpg | 400 | 2014-05-27 11:59:14 |
+-----+--------------+------------+---------------------+
Photo_link :
+------+------+
| plid | pid |
+------+------+
| 100 | 1 |
| 100 | 2 |
| 100 | 3 |
| 101 | 2 |
| 101 | 4 |
+------+------+
Blog Table :
+-----+------------------------+---------------------+------+
| bid | content | created | pid |
+-----+------------------------+---------------------+------+
| 1 | This is my first Blog | 2014-05-27 12:04:44 | 100 |
| 2 | This is my second Blog | 2014-05-27 12:05:01 | 101 |
+-----+------------------------+---------------------+------+
Now i have to insert more than one photo into Blog , so first i have to insert the photos for new blog into photo table and again have to insert all the created photo id (pid's) in photo_link table under one id (for each blog one new id is created for photo_link table to save all photos corresponding to that blog) and insert refer that id in blog table.
My problem is how can i get all the newly created photo ids and then insert into photo_link with first time i have to create new id for new blog and then have to save all the corresponding photo with that id and then insert that photo_link id in blog also.
writing procedure may correct ? if it how to write this procedure ?
please give me some idea on this.
please help me on this.It would be great if this can be done .Thanks in advance.
You need to SELECT SCOPE_IDENTITY after each insert of the newly created photo in order to get the Id of the inserted photo.
Then you can continue your flow based on the id you got.
Example:
DECLARE #insertedId
INSERT INTO Photo(photo_src, photo_size, created) VALUES('/photo/1.jpg', 400, GETDATE())
SELECT #insertedId = SCOPE_IDENTITY()
INSERT INTO Photo_link(content, created, pid) VALUES('This is my first Blog', GETDATE(), #insertedId)
Basically I have two tables A and B. These are linked by unique ID's where the entries in B point to one entry in A. The entries in A and B also have a 'status' field denoting if the entry is active or not...
My questions is therefore; is it possible to link the status field of the entries in B and have them update, every time the 'status' field in A (pointed to by the unique ID) is updated? I could do this fairly easy with an SQL command however I'm wondering if there is a more automatic solution. Example:
table A
|------ID------|----status----|
| 1 | on |
| 2 | on |
|---------------|----------------|
table B
|-----eID------|------ID------|----status----|
| 1 | 1 | on |
| 2 | 1 | on |
| 3 | 2 | on |
|---------------|---------------|----------------|
I then run:
UPDATE `A` SET `status` = 'off' WHERE `ID` = 1;
And the result would be:
table A
|------ID------|----status----|
| 1 | off |
| 2 | on |
|---------------|---------------|
table B
|-----eID------|------ID------|----status----|
| 1 | 1 | off |
| 2 | 1 | off |
| 3 | 2 | on |
|---------------|---------------|----------------|
Is that possible?
Many Regards,
Andreas
i hope this trigger code can help u.
CREATE TRIGGER `abc` AFTER UPDATE ON `tablea` FOR EACH ROW BEGIN UPDATE tableb SET STATUS = new.status WHERE id = new.id;
END
Previous Related Posts:
MySQL: how to convert to EAV?
MySQL: how to convert to EAV - Part 2?
Given a table:
TABLE: foo
===============================
| id | first_name | last_name |
===============================
| 1 | John | Doe |
| 2 | Jane | Smith |
| 3 | Ronald | McDonald |
-------------------------------
How do I take this table and convert it to these tables (an EAV implementation)?:
TABLE: attribute
===========================
| id | fk_id | attribute |
===========================
| 1 | 100 | first_name |
| 2 | 100 | last_name |
---------------------------
TABLE: value
=========================================
| id | attribute_id | row_id | value |
=========================================
| 1 | 1 | 1 | John |
| 2 | 2 | 1 | Doe |
| 3 | 1 | 2 | Jane |
| 4 | 2 | 2 | Smith |
| 5 | 1 | 3 | Ronald |
| 6 | 2 | 3 | McDonald |
-----------------------------------------
NOTES:
attribute.fk_id will be provided.
value.row_id is used to identify how the values are grouped as records in the original table.
UPDATE: Also, how do I query the EAV tables so that I can make it look like table foo again.
I give +1 to #Phil's solution for populating the EAV table. Insert one attribute at a time.
Here's another solution to reverse an EAV transformation:
SELECT v.row_id AS id,
MAX(IF(a.attribute='first_name',v.value,NULL)) AS first_name,
MAX(IF(a.attribute='last_name',v.value,NULL)) AS last_name
FROM value v INNER JOIN attribute a
ON v.attribute_id = a.id
GROUP BY v.row_id
Except that by using EAV, you've put all your values into a column of VARCHAR(255) or whatever, so you have lost information about the respective data types of the original columns.
There's really no way to do it "dynamically" without hard-coding the attribute names, either as joins as #Phil shows, or as columns as I show. It's essentially the same problem as trying to write dynamic pivot queries.
I have written more about EAV in my presentation Practical Object-Oriented Models in SQL and in my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.
I think your only hope is if you use the foo table. bar is essentially useless without the ID.
Try something like this (assuming attribute.id is an auto-increment primary key)
INSERT INTO `attribute` (`fk_id`, `attribute`)
VALUES (100, 'first_name');
INSERT INTO `value` (`attribute_id`, `row_id`, `value`)
SELECT LAST_INSERT_ID(), `id`, `first_name`
FROM `foo`;
INSERT INTO `attribute` (`fk_id`, `attribute`)
VALUES (100, 'last_name');
INSERT INTO `value` (`attribute_id`, `row_id`, `value`)
SELECT LAST_INSERT_ID(), `id`, `last_name`
FROM `foo`;
To reconstruct the foo table, try this
SELECT
`fn`.`row_id` AS `id`,
`fn`.`value` AS `first_name`,
`ln`.`value` AS `last_name`
FROM `value` `fn`
INNER JOIN `attribute` `fn_a`
ON `fn`.`attribute_id` = `fn_a`.`id`
AND `fn_a`.`attribute` = 'first_name'
INNER JOIN `value` `ln`
ON `fn`.`row_id` = `ln`.`row_id`
INNER JOIN `attribute` `ln_a`
ON `ln`.`attribute_id` = `ln_a`.`id`
AND `ln_a`.`attribute` = 'last_name'
Ergh, thanks for reminding me why I hate this pattern