質問

I am having a bit of a problem with some mysql. I will try to give a much background as I can. This is the situation, I had created an availability for my parent's plant nursery and stored the plant information in mysql, I did a terrible job the first time and now I am redoing it with the new knowledge I have gained in the mean time.

Background of the problem, I had 2 tables in the old database one being called Gallery with the following fields:

(id, Plant_id_1, Plant_id_2, Plant_id_3, Plant_id_4, Plant_id_5, Image1, Image1_Copyright, Image2, Image2_Copyright, Scientific, Common, Category, Height, Spread, Exposure, Zone, Descp).

The Plant_id_1... fields would be the id of the second table which is called Availability with the following fields:

(id, name, size, description, available, upcomming, price)

So as you can see I knew nothing about datases(still might not) but on to the new structure for the table, again 2 tables the first being called plants with the following fields:

(id, scientific_name, common_name, category, height, spread, exposure, usda, description, image1_url, image1_copyright, image2_url, image2_copyright)

The second being called current with the following fields:

(id, plant_id, size, available, upcoming, price, description)

The plant_id will be the id of the corresponding plant in the plant table.

Now on to the problem. I need to get the data from the old table and put it into the new table. I have all the plants from gallery in the new plant table no issue there. But now to get the Availability into the Current table.

This is an example from the export of the Availability (I took the top 3):

INSERT INTO `Availability` (`id`, `name`, `size`, `description`,`available`, `upcomming`, `price`) VALUES(91, 'Acer P.''Shishigashira''', ' #5', '30-36"', 27, 0, 36.00); 

INSERT INTO `Availability` (`id`, `name`, `size`, `description`, `available`,`upcomming`, `price`) VALUES(697, 'Arbutus U. ''Compacta''', ' #5','', 0, 0, 16.20); 

INSERT INTO `Availability` (`id`, `name`, `size`, `description`, `available`, `upcomming`, `price`) VALUES(90, 'Acer P.''Shigitatsusawa''', '#15', '', 0, 0, 65.00);

I need to get the plant_id from the plant table and put that into a insert statement, which I believe I have with the following:

INSERT INTO `current` (`plant_id`, `size`,`description`, `available`, `upcoming`, `price`) VALUES(
(SELECT  `id` 
FROM  `plants` 
WHERE  `scientific_name` 
REGEXP  'Acer P.+ ''Shishigashira'''),  ' #5', '30-36"', 27, 0, 36.00);

But now I have a new problem what if there is no plant in the plant table to match, So i need a ifnull in there. to create the plant with the scientific name and the rest null. well I am new to SQL so this is what I have and its not working:

INSERT INTO `current` (`plant_id`, `size`,`description`, `available`, `upcoming`, `price`) VALUES(
(IFNULL( SELECT IFNULL( (SELECT `id` 
FROM  `plants` 
WHERE  `scientific_name` 
REGEXP  'Chamaecyparis O.+ Nana'),( 
INSERT INTO  `plants` (`scientific_name`)
VALUES (Chamaecyparis O. 'Nana'))))),  ' #1', '', 0, 0, 9.25);

As you can see Its very complicated if you think you are up to the challenge of helping me i would beyond greatly appreciated. If no one can help me I will have to manually reenter all the data into the current table.

役に立ちましたか?

解決

You should work towards a select that returns the data you need to insert into your new table. Once you have that you can simply wrap that into a INSERT INTO - SELECT contruction. That way you don't have to export and do individual inserts. You can simply copy the whole set over in one go.

If you make sure that when you copy plants from the gallery into the plants table the id's stay intact I expect a select like this should return the rows you want to insert:

SELECT 
   Gallery.id as plant_id, 
   Availability.size, 
   Availability.available, 
   Availability.upcoming, 
   Availability.price,
   Availability.description
FROM Gallery 
INNER JOIN Availability
ON Gallery.Plant_id1 = Availability.id

Then you can do the same thing 5 times where you change the

ON Gallery.Plant_id1 = Availability.id

to (one by one)

ON Gallery.Plant_id2 = Availability.id
ON Gallery.Plant_id3 = Availability.id
ON Gallery.Plant_id4 = Availability.id
ON Gallery.Plant_id5 = Availability.id

Or you put all of them together in one huge sql statement with a UNION ALL between each select

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top