0

I have a MySQL table that looks like this:

ID    / x_id / x_key  / x_value


322  / 4     / name  / Jack

323  / 5     / name  / Mary

324  / 6     / name  / John

325  / 4     / hide    / 1

326  / 5     / hide    / 0

327  / 6     / hide    / 0


I would like to select the names from the persons which "hide" key corresponds to the "0" value. Here these selected "x_values" would then be Mary and John

To do so, I have the x_id that I can compare between records.

Which x_id's correspond to an x_ key="hide" that matches an x_value = "0"?

Both x_id's 5 and 6.

Which "x_values" are corresponding to these two x_id's where the x_key="name"? Mary and John

In other words, I try to get a single query that would mix these two queries in order to get Mary and John only:

Query A:

SELECT 
    x_id,
    x_value 
FROM 
    mytable 
WHERE 
    x_key='name' 

Query B:

SELECT 
    x_id 
FROM 
    mytable 
WHERE 
    x_key='hide' 
AND 
    x_value='0'

I just don't find the correct way to do that. How can I?

I'm really sorry for the explanation but I'm not english and it is very hard to explain.

4
  • 1
    What is your table stucture? Nov 7, 2015 at 19:02
  • id - x_id - x_key - x_value. Thank you
    – Baylock
    Nov 7, 2015 at 19:02
  • 1
    x_id is a foreign key ... to which table? Nov 7, 2015 at 19:11
  • Sorry, I edited my post to make things clearer.
    – Baylock
    Nov 7, 2015 at 20:04

3 Answers 3

1

If i have understood you correct you want to select the elements that have a specific x_key with x_value = '0' and that are not hidden (x_key != 'hide').

EDIT (according to your edit):

SQL Fiddle

SELECT bb.x_value
FROM mytable AS aa
INNER JOIN mytable AS bb
ON aa.x_id = bb.x_id
WHERE aa.x_key = 'hide' AND aa.x_value = 0 AND bb.x_key = 'name';

OLD ANSWER (before your edit):

SELECT x_id, x_key, x_value
FROM mytable
WHERE x_key='name' AND x_key != 'hide' AND x_value = '0'
2
  • 1
    If you mean something different then please update your question accordingly giving more information. Nov 7, 2015 at 19:05
  • I edited the question. I hope this time it will be clearer. I really do my best.
    – Baylock
    Nov 7, 2015 at 20:02
1

You should use join to connect two instances of the table - one for the names and one for the 'hides'

select n.x_id, n.x_value from mytable as h inner join myable as n on 
h.x_id = n.x_id where n.x_key = 'name' and h.x_key = 'hide' and H.x_value = 0;

while this will work, I think it's not a good practice to have two types of data in the same table. I'd recommend you to split it to two tables- one for names and one for hides

1
  • Thank you for your help. I'm trying to retrieve data from this table, I'm not the one who created it. In fact, this is the Wordpress postmeta table... It's a generic table that plugins creators can take advantage o without messing with the database. It's not ideal but it allows to insert almost everything (key=> value) without the need to change the table structure itself. I'll try your query and will get back to you. Thank you.
    – Baylock
    Nov 7, 2015 at 20:30
0

If I understand you correct you want combine 2 'SELECT' with different 'WHERE' statments. You can use 'OR' statment

SELECT x_id,x_value FROM mytable WHERE x_key='name' OR x_key='hide' AND x_value='0'
1
  • That's not it but thank you anyway. See the updated question if you want to know what it was all about.
    – Baylock
    Nov 8, 2015 at 2:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.