SQL exclude first ID entry - php

I need to exclude first id entry, and display the rest on my wordpress, I have never used sql before.
$all_users_id = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC", $sort ));

One way would be to use something like
SELECT *
FROM Table
WHERE ID > (SELECT MIN(ID) FROM Table)

I am not familiar with Wordpress, but try this:
$all_users_id = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users WHERE $wpdb->users.ID NOT IN (SELECT MIN($wpdb->users.ID) FROM $wpdb->users) ORDER BY %s ASC", $sort ));

$all_users_id = $wpdb->get_col( $wpdb->prepare("SELECT `$wpdb->users.ID` FROM `$wpdb->users` WHERE `$wpdb->users.ID` != 1 ORDER BY %s ASC", $sort ));
The query used:
SELECT `$wpdb->users.ID`
FROM `$wpdb->users`
WHERE `$wpdb->users.ID` != 1
ORDER BY %s ASC
Fast and simple... Without sub-querys! :)

If it's a mysql db then use the limit clause:
SELECT `$wpdb->users.ID`
FROM `$wpdb->users`
ORDER by $sort ASC
LIMIT 1,9999999
C.

Related

Can i write two foreach in only one?

In my code I have this:
$im = $database->query("SELECT * FROM cms_messaggi_importanti WHERE messo_da = :id ORDER BY ID DESC", array("id"=>$functions->Utente("id")));
foreach($im as $imp){
$mex_i = $database->query("SELECT * FROM cms_messaggi WHERE id = :id ORDER BY ID DESC", array("id"=>$imp['id_mex']));
foreach($mex_i as $mex_imp){
}
}
Can I write this code in only one? Because I have to use a lot of variable with this method. Is there a solution to my problem? For example, using "JOIN"?
You can (and should) do your query in one go, and then iterate over those results:
$im = $database->query("
SELECT *
FROM cms_messaggi_importanti mi
LEFT JOIN cms_messaggi m ON m.id = mi.id_mex
WHERE messo_da = :id
ORDER BY mi.ID DESC,
m.ID DESC",
array("id"=>$functions->Utente("id")));
foreach($im as $imp){
//...
}
You will probably need to replace the SELECT * by a more precise column list, which will be the columns available in the result set.

Querying WordPress with SELECT SQL_CALC_FOUND_ROWS

So, I've recently taken on a job making changes to the WordPress theme for arena . govspace . gov . au and I've been asked to look into why posts posted on the same day do not appear in order according to the time they were posted.
Now, the way the content is pulled in is not the way I am used to doing it in WordPress. I would normally use a WP_Query, so the examples below are a little out of my wheelhouse.
This function is responsible querying for the output for this page: https://arena.govspace.gov.au/news-media/media-releases/
function SQLQueryPost( $post_type,$meta_key,&$paged,&$rows,&$total_row, $limit_query = "limit 10"){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wpdb;
$test = $wpdb->posts;
$sql = 'SELECT SQL_CALC_FOUND_ROWS meta_value, id, post_title
FROM '.$wpdb->posts.' join '.$wpdb->postmeta.' on '.$wpdb->posts.'.id = '.$wpdb->postmeta.'.post_id
WHERE
meta_value != "" and meta_key = "'.$meta_key.'"
and post_status = "publish"
and post_type = "'.$post_type.'"
order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC
'.$limit_query.' offset '.($paged - 1 )*10;
$rows = $wpdb->get_results($sql);
$total_row = $wpdb->get_var( "SELECT found_rows();" );
}
For this query, I simply changed the line order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC to order by post_date DESC and magically it now orders them by date and time.
This function is responsible for pulling more than one content type for this page: https://arena.govspace.gov.au/news-media/
function SQLQueryPostMulti( $post_types,$meta_key,&$paged,&$rows,&$total_row, $limit_query = "limit 10"){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wpdb;
$test = $wpdb->posts;
$sql = 'SELECT SQL_CALC_FOUND_ROWS meta_value, id, post_title, post_type
FROM '.$wpdb->posts.' join '.$wpdb->postmeta.' on '.$wpdb->posts.'.id = '.$wpdb->postmeta.'.post_id
WHERE
meta_value != ""
and post_status = "publish"
and post_type IN (' . $post_types . ')
order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC
'.$limit_query.' offset '.($paged - 1 )*10;
$rows = $wpdb->get_results($sql);
$total_row = $wpdb->get_var( "SELECT found_rows();" );
}
This query seems to be a little trickier. I attempted the same change (order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC to order by post_date DESC) but in this case the page returns the exact same entry for all 10 results.
Basically, if anyone could provide any kind of insight as to how I can make this query not only order the posts by day, but to also order them by time posted on that day, I would be extremely grateful.

Adding AVG(Price) into a mySQL query?

I have the following query
$query = "SELECT * FROM used ".$conditionString." ORDER BY id DESC LIMIT $offset, $rec_limit";
But now I want to know also the average price of the returned rows
AVG(Price) AS PriceAverage
How to do this?
this should work for you:
$query = "SELECT *,AVG(Price) FROM used ".$conditionString." ORDER BY id DESC
GROUP BY id WITH ROLLUP
LIMIT $offset, $rec_limit";
demo: http://sqlfiddle.com/#!2/4938f/1/0

Custom select query wordpress order by meta key value

I am trying to ORDER BY my results of a custom select query.
But I am trying to order by the value of a meta key.
Please see my query below...
$get_atts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->posts.meta_key = 'surname' ASC");
As you can see this ORDER BY is breaking it...
ORDER BY $wpdb->posts.meta_key = 'surname' ASC"
So I am trying to order by the value of surname
But my does not seem to work. Can any explain why or help?
Try to use this query:
$get_atts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'surname'
AND $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->postmeta.meta_value ASC");

MYSQL/PHP: How do you Order By Starting point in a string?

SELECT
SQL_CALC_FOUND_ROWS posts.*,
CASE
WHEN postmeta.meta_value REGEXP '$regex'
THEN 1
ELSE 0
END AS keyword_in_title,
MATCH ( posts.post_content )
AGAINST ( '".addslashes( $s )."' ) AS mysql_score
FROM
$wpdb->posts as posts,
$wpdb->postmeta as postmeta
WHERE
posts.post_date_gmt <= now()
AND postmeta.post_id = posts.ID
AND postmeta.meta_key='_headspace_page_title'
AND posts.post_password = ''
AND posts.post_status = 'publish'
AND posts.post_type != 'attachment'
AND ( postmeta.meta_value REGEXP '$regex'
OR posts.post_content REGEXP '$regex')
GROUP BY
posts.ID
ORDER BY
charindex($s, 'keyword_in_title') DESC
LIMIT
$offset,
$limit
As to the order issue (Apart from the escaping issue #Gumbo pointed out in the comment), CHARINDEX is not a valid mysql string function. The LOCATE function looks to be identical (at least from a cursory glance between the docs on SQLServer and MySQL)...
$s = mysql_real_escape_string($s);
$order = 'ORDER BY LOCATE(\''.$s.'\', `keyword_in_title`) DESC';
I'm assuming that the code above does not work and hence you're asking a question.
Try adding charindex($s, 'keyword_in_title') AS cOrder in your SELECT clause and then ORDER BY cOrder DESC
Good luck!

Categories