Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-27
7
Medium Priority
?
373 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
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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

782 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