I have a function which runs daily which I want to check if a post is expired (based on a custom postmeta field) and if so set the post status to draft. I think I have the principle right but there's something probably pretty wrong with the syntax which isn't making it work correctly. This is the function so far:
function make_product_draft( ) {
global $wpdb;
$sql = "
UPDATE $wpdb->posts
INNERJOIN $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id
SET $wpdb->posts.post_status = 'draft'
WHERE $wpdb->posts.post_type = 'product' AND $wpdb->posts.post_status = 'publish' AND $wpdb->postmeta.post_type = 'expired'
";
$wpdb->query($sql);
}
I think where I am going wrong is when I do this: $wpdb->posts.ID? I am not sure what the correct syntax is. Your help is much appreciated :)
My requirements changed since I made the post, however the following code worked for me. Thanks Niklaz for the comment, your assistance was pivotal in getting it working right :)
function make_product_draft( ) {
global $wpdb;
$sql = "UPDATE {$wpdb->posts}
INNER JOIN {$wpdb->postmeta} on {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
SET {$wpdb->posts}.post_status = 'draft'
WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' AND {$wpdb->postmeta}.meta_key = '_product_expiry' AND {$wpdb->postmeta}.meta_value < CURDATE()
";
$wpdb->query($sql);
}
Related
What is the correct/efficient way to display results based on a variable if the variable is set? If variable is not set, the AND operator should not be used.
I apologize if this is a repeat, I clicked the suggested links and they did not make sense to me.
Near the end of code is my note with ^^^^^ marked.
For example:
$whatever = 123;
SELECT
DISTINCT terms.name as product_type_name,
tax.term_id as termidouter
FROM $wpdb->posts AS p
INNER JOIN wp_term_relationships r
ON p.ID = r.object_id
INNER JOIN wp_term_taxonomy tax
ON r.term_taxonomy_id = tax.term_taxonomy_id
INNER JOIN wp_terms terms
ON tax.term_id = terms.term_id
WHERE
tax.taxonomy = 'product_type'
AND p.post_status = 'publish'
AND p.post_type = 'product'
AND '$whatever ' = terms.term_id
^^^^ If $whatever is empty, I want to return results as if this line did not exist.
ORDER BY product_type_name
");
I was going to do an IF/ELSE but I figured that was the lazy way.
$whatever = 123;
if (empty($whatever)) {
// SELECT without the AND
} else {
// SELECT with the AND
}
You could do something like this:
AND CASE WHEN '$whatever ' IS NOT NULL THEN '$whatever ' ELSE terms.term_id END = terms.term_id
So... typically you'd want to use prepared statements, but if we're going this route, I would collect all my optional search criteria in an array as strings:
$myTerms=array();
if(!empty($whatever)) {
$myTerms[]="terms.term_id='" . $whatever . "'";
}
...
Then you can build your query easily like so:
$mySql = "SELECT * FROM whatever WHERE somefield='somevalue' ";
if(count($myTerms)>0) {
$mySql.=" AND " . implode(" AND ", $myTerms);
}
Please note that this is a basic example; you should also be checking any value you pass into your query for attacks etc.
I'm attempting to join two tables while using codeigniter. I've done this same SQL query writing regular SQL. When I attempt to do the same in codeigniter, I keep getting errors. I'm not quite sure what I'm doing wrong. What do you guys think I'm doing wrong?
My function in model_data.php
function getJoinInformation($year,$make,$model)
{
//$this->db->distinct();
// here is where I need to conduct averages but lets just work on extracting info.
// from the database and join tables.
$this->db->select('*');
$this->db->from('tbl_car_description');
$this->db->join('tbl_car_description', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$result = $this->db->get();
/*
$query = $this->db->get_where('tbl_car_description',
array(
'year' => $year,
'make' => $make,
'model' => $model
)
);
if($query->num_rows()) return $query->result();
return null;
*/
}
My error message
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'tbl_car_description'
SELECT * FROM (`tbl_car_description`) JOIN `tbl_car_description` ON `d`.`id` = `p`.`cardescription_id` WHERE `d`.`year` = '2006' AND `d`.`make` = 'Subaru' AND `d`.`model` = 'Baja'
Filename: C:\wamp\www\_states\system\database\DB_driver.php
Line Number: 330
Here is the code written in SQL and it's working great. I want to do the something in codeigniter but I'm confused as to how. Any help would be much appreciated. Thanks everyone.
$sql_2 = mysql_query("SELECT ROUND(AVG(p.value),1) AS AvgPrice, ROUND(AVG(p.mileage),1) AS AvgMileage
FROM tbl_car_description d, tbl_car_prices p
WHERE (d.id = p.cardescription_id)
AND (d.year = '".$year."')
AND (d.make = '".$make."')
AND (d.model = '".$model."')
AND (p.approve = '1')");
Your CI query references the same table twice, which I assume is a typo. However, you only need include your table alias with the table name in your Active Record call:
//$this->db->distinct();
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->join('tbl_car_prices p', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$result = $this->db->get();
Couple issues: You need to include your table aliases and your join should have the name of the second table ...
$this->db->from('tbl_car_description AS d');
$this->db->join('tbl_car_prices AS p', 'd.id = p.cardescription_id');
Maybe I am doing something wrong here, but I am unable to join on multiple tables when using createBuilder(). Here is an example query.
$test = $this->modelsManager->createBuilder()
->from('TABLE1')
->leftJoin('TABLE2', 'TABLE1.id = TABLE2.table_one_id')
->leftJoin'TABLE3', 'TABLE3.id = TABLE2.table_three_id')
->where('TABLE1.id = :id:', array('id' => $id))
->groupBy(array('TABLE1.id'))
->getQuery()
->execute();
There error indicates that the SQL query is probably not being generated correctly by the framework, but I could very well be doing something wrong. It appears that no space is being added before the additional LEFT join.
Unknown column 'TABLE2.table_one_idLEFT' in 'on clause''
Any insight would be greatly appreciated.
You will need to add a space at the end of first join condition:
$test = $this->modelsManager->createBuilder()
->from('TABLE1')
->leftJoin('TABLE2', 'TABLE1.id = TABLE2.table_one_id ')
->leftJoin('TABLE3', 'TABLE3.id = TABLE2.table_three_id')
->where('TABLE1.id = :id:', array('id' => $id))
->groupBy(array('TABLE1.id'))
->getQuery()
->execute();
I´m working with Symfony2 and I need to execute this SQL for example:
select detformacion.* from detformacion
left join formacion
on detformacion.formacion_id = formacion.id
left join detcurso
on formacion.id = detcurso.formacion_id
where detcurso.id IN ('143','144');
For that, I have this in my Repository:
public function getSeleccion() {
$em = $this->getEntityManager();
$query = $em->createQueryBuilder()
->select('d')
->from('GitekUdaBundle:Detformacion', 'd')
->leftJoin('d.formacion', 'f')
->leftJoin('f.detcursos', 'det')
->where('det.id = :miarray')
->setParameter('miarray',array('143','144'))
->getQuery()
;
return $query->getResult();
}
I tried with ->where('det.id IN :miarray') but I´m getting errors all the time.
Any help or clue?
thanks in advance.
UPDATE: The problem is setting the parameters.
Missing parentheses after the IN operator :
->where('det.id IN (:miarray)')
->setParameter('miarray', array('143','144'))
I am new to zend framework,
Following is the plain mysql query which takes particular column from table,
SELECT jobs_users.id,jobs_users.first_name from jobs_users left join friends on jobs_users.id=friends.friend_id where friends.member_id=29
I tried with zend to implement the above query like below,
public function getFriendsProfileList($id){
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from('jobs_users')
->joinLeft(
'friends',
'jobs_users.id=friends.friend_id',
array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo')
)
->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;
}
Here i got result with all column name , not with exact column name which i have given in query.
Kindly help me on this.
Use this instead:
$select->from('jobs_users', array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'))
->joinLeft('friends', 'jobs_users.id=friends.friend_id')
->where("friends.member_id = ?", '20');
You may also try this:
$select = $db->select();
$select->setIntegrityCheck(false);
$select->joinLeft('jobs_users','',array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'));
$select->joinLeft('friends','jobs_users.id=friends.friend_id', array());
$select->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;