Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mySQL query needed:  find an ID where two (meta_key) rows match

Posted on 2014-07-27
7
Medium Priority
?
369 Views
Last Modified: 2014-07-28
there's one meta table   tblOrdersMeta   with various meta_keys per orderitem_ID

tableID    orderitem_ID   meta_key     meta_value
     1               10                    productID        123
     2               10                    orderID             456

what is the query to find the orderitem_ID  (ex: 10)   that has in common  productID=123   and    orderID=456
(tableID is irrelevant for the end result)

(i can put in the appropriate PHP variables etc)
0
Comment
Question by:willsherwood
[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
7 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40222951
If you know the orderitem_ID, then this.

SELECT * FROM tblOrdersMeta WHERE orderitem_ID = 10;

If not then you need to do a query on one of the other items to get it.
0
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 800 total points
ID: 40223011
Try this (untested):
SELECT orderitem_ID FROM 
(
(SELECT orderitem_ID, meta_key, meta_value FROM tblOrdersMeta 
WHERE meta_key = 'productID' AND meta_value = '123') t1
JOIN 
(SELECT orderitem_ID, meta_key, meta_value FROM tblOrdersMeta 
WHERE meta_key = 'orderID' AND meta_value = '456') t2
ON t1.orderitem_ID=t2.orderitem_ID
)

Open in new window

The idea is to select all the rows with productID=123, then all the rows with orderID=456, then INNER JOIN them together on orderitem_ID, which will give you all the common orderitem_ID.

HTH,
Dan
0
 

Author Comment

by:willsherwood
ID: 40223019
orderitemID is what i'm looking for.
something like:

WHERE
      (select orderitemID FROM...  WHERE   meta_key='productID' AND meta_value=123 )
=
      (select orderitemID FROM...  WHERE   meta_key='orderID' AND     meta_value=456 )
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

Author Comment

by:willsherwood
ID: 40223022
Dan - i'll give it a try (my reply to Dave crossed paths)
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 40223079
If you want to get a little bit of PHP into the mix you might try this strategy.

1. SELECT orderitem_id and create an array of all of them.
2. Use array_not_unique() to find the replicants.
3. Iterate over the replicants to SELECT the rows with replicated orderitem_id values.

<?php // demo/array_not_unique.php
error_reporting(E_ALL);
echo "<pre>";

// A FUNCTION TO FIND REPLICATED VALUES IN AN ARRAY
function array_not_unique($raw)
{
    // MAN PAGE: http://php.net/manual/en/function.array-count-values.php
    $new = array_count_values($raw);
    foreach ($new as $key => $val)
    {
        if ($val < 2) unset($new[$key]);
    }
    return $new;
}


// SOME TEST DATA
$raw_array   = array();
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'def@xyz.com';
$raw_array[] = 'ghi@xyz.com';
$raw_array[] = 'jkl@xyz.com';
$raw_array[] = 'mno@xyz.com';
$raw_array[] = 'pqr@xyz.com';
$raw_array[] = 'stu@xyz.com';

// SOME DUPLICATES
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'jkl@xyz.com';
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'def@xyz.com';


// SHOW THE FUNCTION AT WORK
$common = array_not_unique($raw_array);
foreach ($common as $x => $n)
{
    echo PHP_EOL . "THE VALUE $x APPEARED $n TIMES";
}

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 800 total points
ID: 40223167
http://sqlfiddle.com/#!2/b0050/1

SELECT t1.orderitem_ID from tblOrdersMeta t1
inner join tblOrdersMeta t2
ON t1.orderitem_ID=t2.orderitem_ID
where t1.meta_key='productID' 
AND t1.meta_value=123
AND t2.meta_key='orderID' 
AND t2.meta_value=456

Open in new window

0
 

Author Closing Comment

by:willsherwood
ID: 40224570
thanks all!
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…
Suggested Courses

705 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