?
Solved

MySQL Insert

Posted on 2011-03-04
2
Medium Priority
?
391 Views
Last Modified: 2012-05-11
I am have a brain fart trying to figure out the logic to perform a particular query. Looking for assistance!

The following table is used to store all the items that a child has in his chest. As you can see, a child can have multiple items.

child_id     item_id     quantity
1               1              1
2               1              1
2               1              1
2               4              1
2               4              1
3               8              1
3               9              1
4               2              1

What I am trying to do is give every child item #8 that don't already have it. My first attempt at this query is show in the code section below. However, this doesn't work very well, as it will still return records if the child has other options.
INSERT child_items (child_id, item_id, quantity)
SELECT DISTINCT child_id, 8, 1 
FROM child_items WHERE item_id != 8;

Open in new window

0
Comment
Question by:plecostomus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35038412
what about:
INSERT child_items (child_id, item_id, quantity)
SELECT child_id, 8, 1 
FROM child_items 
GROUP BY child_id
HAVING MAX(CASE WHEN item_id = 8 THEN 1 ELSE 0 END) = 0

Open in new window


or:
INSERT child_items (child_id, item_id, quantity)
SELECT child_id, 8, 1 
FROM child_items i
WHERE NOT EXISTS( SELECT NULL FROM child_items o WHERE o.child_id = i.child_id AND o.item_id = 8 )
GROUP BY child_id

Open in new window


however, not sure if it will actually work in MySQL, you might run into the issue that you cannot select from the same table you do the insert into,
so you might need to run the insert into a temp table first ...

0
 

Author Closing Comment

by:plecostomus
ID: 35038632
Worked perfectly... MySQL 5.0/5.1 allows you to insert into itself from itself.
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question