Solved

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

Posted on 2014-07-27
7
355 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 83

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 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 200 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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 200 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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