Solved

Wordpress Select Syntax

Posted on 2014-04-11
9
405 Views
Last Modified: 2014-04-12
I have the following wordpress table data.
The columns are named as follows:
Meta_id
user_id
meta_key
meta_value
15       2       first_name       Robert
16       2       last_name       Granlund
17       2       nickname       rgranlund
18       2       description       
19       2       rich_editing       true
20       2       comment_shortcuts       false
21       2       admin_color       fresh
22       2       use_ssl       0
23       2       show_admin_bar_front       true
24       2       wp_capabilities       a:1:{s:13:"administrator";b:1;}
25       2       wp_user_level       10

I want to write this but I'm not sure how:
$sql = "SELECT meta_value WHERE meta_key ='first_name', 
meta_value WHERE meta_key ='last_name', 
meta_value WHERE meta_key ='wp_user_level', 
FROM wp_usermeta
GROUP BY user_id
"

Open in new window


Right now there is only one user_id "2"  But if there was 100 user id I want it to return all 100, grouped by the user_id.  Any hints?
0
Comment
Question by:rgranlund
  • 4
  • 4
9 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39994951
Try this:
SELECT user_id, meta_key, meta_value FROM wp_usermeta
WHERE meta_key IN ('first_name', 'last_name', 'wp_user_level')

Open in new window

HTH,
Dan
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39995218
@dan, it pulled the user_id but not the first_name last_name.

Any further ideas?

I tried this:
				$sql_b = 	"SELECT user_id, meta_key, meta_value
							FROM wp_usermeta
							WHERE meta_key IN('first_name' AS first_name AND 'last_name' AS last_name)
							GROUP BY user_id";
				
				$pdos_b = $pdo->prepare($sql_b, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39995224
You can't do AS in the IN clause. It makes no sense.

And you don't need the GROUP BY.

The expected result of my SELECT is:
2       first_name            Robert
2       last_name             Granlund
2       wp_user_level    10
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39995237
Here is the rest of it and keep in mind that there will be multiple users :

				$sql_b = 	"SELECT user_id, meta_key, meta_value FROM wp_usermeta
WHERE meta_key IN ('first_name', 'last_name', 'wp_user_level')";
				
				$pdos_b = $pdo->prepare($sql_b, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
			
			try {
    			$pdos_b->execute();
	//  GET RESULTS
				$bg = '#eeeeee'; 

				while($row_b = $pdos_b->fetch()):
				$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

					echo '<tr bgcolor="' . $bg . '">
						<td align="left"><a href="delete_user.php?id=' . $row_b->user_id . '"><img src="'.$path.'img/buttons/delete.png"/></a></td>
						<td align="left"><a href="edit_user.php?id=' . $row_b->user_id . '"><img src="'.$path.'img/buttons/edit-icon.png"/></a></td>
						<td align="left">' . $row_b->last_name . '</td>
						<td align="left">' . $row_b->first_name . '</td>
						<td align="left">' . $row_b->wp_user_level . '</td>
					</tr>';
			 	endwhile;	
			} 
			catch(PDOException $a) {
   				echo 'ERROR: ' . $a->getMessage();
			}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39995325
You will get 3 rows for every user with my original query. For what you want you need a pivot (to transform data from rows into columns).

Try this:
SELECT `user_id`, MAX(`first_name`) AS `first_name`, MAX(`last_name`) AS `last_name`, MAX(`wp_user_level`) AS `wp_user_level` FROM (
SELECT `user_id`, 
	CASE WHEN `meta_key` = 'first_name' THEN `meta_value` END AS `first_name`,
	CASE WHEN `meta_key` = 'last_name' THEN `meta_value` END AS `last_name`,
	CASE WHEN `meta_key` = 'wp_user_level' THEN `meta_value` END AS `wp_user_level`
FROM wp_usermeta) pivot
GROUP BY `user_id`

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39995454
no points pl

Assuming it's MySQL then Dan's query above looks great.

Just one tiny tip, he has used an alias "pivot" and while this is not (currently) a reserved word in MySQL it is in other SQL dialects - and might become one in MySQL one day.

So I'd replace the word "pivot" with something else, "sq" (for sub query) for example.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39996278
@dan & Paul, The following works.  The above did not however, your examples got me going in the correct direction, thank you!

$sql_b = "SELECT
    u1.ID,
    u1.user_email,
    u1.user_registered,
    m1.meta_value AS first_name,
    m2.meta_value AS last_name,
    m3.meta_value AS wp_user_level
FROM wp_users u1
JOIN wp_usermeta m1 ON (m1.user_id = u1.ID AND m1.meta_key = 'first_name')
JOIN wp_usermeta m2 ON (m2.user_id = u1.ID AND m2.meta_key = 'last_name')
JOIN wp_usermeta m3 ON (m3.user_id = u1.ID AND m3.meta_key = 'wp_user_level')";

	
		$pdos_b = $pdo->prepare($sql_b, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
			
		try {
    		$pdos_b->execute();
	//  GET RESULTS
			$bg = '#eeeeee'; 

			while($row_b = $pdos_b->fetch()):
				
				$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
				echo '<tr bgcolor="' . $bg . '">
					<td align="left"><a href="delete_user.php?id=' . $row_b->user_id . '"><img src="'.$path.'img/buttons/delete.png"/></a></td>
					<td align="left"><a href="edit_user.php?id=' . $row_b->user_id . '"><img src="'.$path.'img/buttons/edit-icon.png"/></a></td>
					<td align="left">' . $row_b->last_name . '</td>
					<td align="left">' . $row_b->first_name . '</td>
					<td align="left">' . $row_b->wp_user_level . '</td>
				</tr>';
			 endwhile;	
		} 
		catch(PDOException $a) {
   			echo 'ERROR: ' . $a->getMessage();
		}

Open in new window

0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 39996279
It was not exactly what I needed but it pointed me in the correct direction.  Thanks you!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39996436
Glad I could help!

I personally tested my query on a Wordpress site and it's working.

However, your approach using JOIN's is faster than a CASE.
Tested it on a 10 user db and the results are:
1. my query: Showing rows 0 - 9 (10 total, Query took 0.0083 sec)
2. your query: Showing rows 0 - 9 (10 total, Query took 0.0018 sec)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should I use subdomains or addon domains? 3 36
PHP warning 4 30
datetime in sql 6 29
mysql between clause 2 8
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

14 Experts available now in Live!

Get 1:1 Help Now