Solved

SQL Insert Query Help, part2

Posted on 2016-10-05
6
41 Views
Last Modified: 2016-10-05
This is related to the schema/sample data already posted on this question:
here


I have 2 rows that need to be added to a table, `wp_posts` that have a count for the number of contacts for a given post_id.   One row will have a meta_key of 'contacts' and the meta_value will be the count.  The 2nd row meta_key will be '_contacts' and meta_value will always be 'field_5790aaada7333'.  The post_id will be the same for both rows.  So for example, using the sample data posted on the other question, the rows would look like this when finished:
post_id 	meta_key 	meta_value
3001 	contacts 	5
3001 	_contacts 	field_5790aaada7333
3003 	contacts 	1
3003 	_contacts 	field_5790aaada7333
3005 	contacts 	2
3005 	_contacts 	field_5790aaada7333
3008 	contacts 	4
3008 	_contacts 	field_5790aaada7333

Open in new window


The other issue with this is that the rows may exist for a given already, and if so only the first row need to be updated with the actual number of contacts.  My thought was to count the number of rows where the meta_key is like one of the meta_keys that is static, i.e. field_5790aac3a7334, for a given post_id, however I am unsure of how to either update the value of a row or insert 2 new rows if the rows do not exist.
0
Comment
Question by:bdhtechnology
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:bdhtechnology
Comment Utility
Actually there are only 7 rows where the fields exist already, so the part regarding the update portion of the query can be ignored, I just need to add 2 rows for each post_id to count the number of contacts.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
can this be a separate query?

I am about to finish up for the night, so thi s is as far as my sleepy eyes would let me go.. hope it helps

SELECT
...
  CASE
         WHEN cj.n = 1 THEN c.contact_count
         WHEN cj.n = 2 THEN 'field_5790aaada7333'
  END AS meta_value
FROM (
    SELECT
         cc.company
       , COUNT(*) contact_count
    FROM `chambercontacts` cc
    GROUP BY
         cc.company
      ) c
...
CROSS JOIN (
          SELECT 1 n UNION ALL
          SELECT 2 n
         ) cj
    

Open in new window

0
 
LVL 1

Author Comment

by:bdhtechnology
Comment Utility
Yes, I have the rows inserted now but to use them I need to have the correct count for the actual number of contacts.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
please refresh page, my lat comment was being changed as you added :)

I'm off to bed I'm afraid way too tired
0
 
LVL 1

Author Comment

by:bdhtechnology
Comment Utility
That was quite helpful, with a few tweaks I was able to finish this up.  Thanks for all of your help!

INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`)
SELECT
  p.ID AS post_id,
  CASE
         WHEN cj.n = 1 THEN 'contacts'
         WHEN cj.n = 2 THEN '_contacts'
  END AS meta_key,
  CASE
         WHEN cj.n = 1 THEN c.contact_count
         WHEN cj.n = 2 THEN 'field_5790aaada7333'
  END AS meta_value
FROM (
  SELECT
     cc.company,
     cc.deleted,
     COUNT(*) contact_count
  FROM `chambercontacts` cc
  WHERE cc.deleted=0
  GROUP BY
       cc.company
  ) c
  LEFT JOIN `directory` d ON c.company=d.id
  LEFT JOIN `wp_posts` p ON d.companyname LIKE p.post_title
  CROSS JOIN (
          SELECT 1 n UNION ALL
          SELECT 2 n
  ) cj
WHERE
  d.deleted=0 AND
  p.ID>=3000;

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Great! Thanks for the question(s). Cheers, Paul
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now