Jump to content

swap column values


Destramic
Go to solution Solved by Barand,

Recommended Posts

hey guys i've tried to run this query on my database but it only partially works...the images paths will change but the values for cover won't budge :-\

 

table

---------------------

item_id

path   - varchar

cover - int(1)

--------------------

 

cover will be 1 or 0 depending if the image is the main image.

 

i can see anything i'm doing wrong here...can someone please shine some light on why both path and cover values are not swapping.

 

here is my sql:

UPDATE item_images im1, item_images im2 
SET im1.cover = im2.cover, 
    im2.cover = im1.cover, 
    im1.path = im2.path, 
    im2.path = im1.path
WHERE im1.item_id = im2.item_id
AND im1.path = '1447700522.jpg'
AND im2.path = '1447700481.jpg'
AND im1.item_id = 58

thank you

Link to comment
Share on other sites

mysql> SELECT * FROM item_images;
+----------------+---------+-------+---------+
| item_images_id | item_id | cover | path    |
+----------------+---------+-------+---------+
|              1 |      58 |     1 | 111.jpg |
|              2 |      58 |     0 | 222.jpg |
+----------------+---------+-------+---------+

Then your query (the names have been changed to protect the innocent)

UPDATE item_images im1, item_images im2 
SET im1.cover = im2.cover, 
    im2.cover = im1.cover, 
    im1.path = im2.path, 
    im2.path = im1.path
WHERE im1.item_id = im2.item_id
AND im1.path = '222.jpg'
AND im2.path = '111.jpg'
AND im1.item_id = 58;

gives

mysql> SELECT * FROM item_images;
+----------------+---------+-------+---------+
| item_images_id | item_id | cover | path    |
+----------------+---------+-------+---------+
|              1 |      58 |     0 | 222.jpg |
|              2 |      58 |     1 | 111.jpg |
+----------------+---------+-------+---------+

which looks OK to me

 

???

  • Like 1
Link to comment
Share on other sites

yeah cover column dosn't move....here is results upon select before sql.

 

 

image_id       item_id         path                                  cover

---------------------------------------------------------------------------

13                   58                1447700481.jpg                 1

14                   58                1447700522.jpg                 0

---------------------------------------------------------------------------

 

after sql execution

 

image_id       item_id         path                                  cover

---------------------------------------------------------------------------

13                   58               1447700522.jpg                 0

14                   58               1447700481.jpg                 1

---------------------------------------------------------------------------

 

here is the table i'm using

CREATE TABLE `item_images` (
	`image_id` INT(11) NOT NULL AUTO_INCREMENT,
	`item_id` INT(11) NOT NULL,
	`path` VARCHAR(130) NOT NULL,
	`cover` INT(1) NOT NULL DEFAULT '0',
	PRIMARY KEY (`image_id`),
	UNIQUE INDEX `item_images_image_id_UNIQUE` (`image_id`),
	INDEX `item_images_item_id_INDEX` (`item_id`),
	CONSTRAINT `item_images_item_id_FOREIGN_KEY` FOREIGN KEY (`item_id`) REFERENCES `items` (`item_id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

somethings not right although it seems it should be...be good to know what's up here.

 

thank you

Link to comment
Share on other sites

Look again!

 

 

before: ID: 14     Cover:1
after:  ID: 14     Cover:0
Both the image and cover are changed.

 

If your objective is to switch which is the main image, just change cover and not the image name (or vice versa)

 

 

what a plonker i am...i took away setting the cover and it worked as i wanted it to :suicide:

UPDATE item_images im1, 
       item_images im2 
SET im1.path = im2.path,  
    im2.path = im1.path 
WHERE im1.item_id = im2.item_id
AND im1.path = :cover_image_path
AND im2.path = :image_path
AND im1.item_id = :item_id

sorry for being a fool! haha...thank you for your help barand

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.