i need to use dynamic table names on model select query like this:
$this->db->select("$this->table_car.id as carId, $this->table_car.price as carPrice, $this->table_car.name as carName, $this->table_car.used as carUsed, $this->table_car.visible as carVisible, $this->table_cat.name as catName, $this->table_brand.name as carBrand, $this->table_model.name as carModel");
But variable is not working. This is what I get:
SELECT .`id` AS `carId`, .`price` AS `carPrice`, .`name` AS `carName`, .`used` AS `carUsed`, .`visible` AS `carVisible`, .`name` AS `catName`, .`name` AS `carBrand`, .`name` AS `carModel`
LEFT JOIN ON .`car_id` = .`id`
LEFT JOIN ON .`id` = .`cat_id`
LEFT JOIN ON .`id` = .`brand_model_id`
LEFT JOIN ON .`id` = .`model_id`
LEFT JOIN ON .`id` = .`brand_id`
WHERE .`used`= 0 LIMIT 3
Is there a workaround for this?
Thanks in advance
Whilst double quotes do allow the use of variables inside them without the need to close and concatenate, it doesn't always work for the likes or object and arrays, like you are showing. A better solution would be:
$this->db->select($this->table_car . ".id as carId, "
. $this->table_car . ".price as carPrice, "
. $this->table_car . ".name as carName, "
. $this->table_car . ".used as carUsed, "
. $this->table_car . ".visible as carVisible, "
. $this->table_cat . ".name as catName, "
. $this->table_brand . ".name as carBrand, "
. $this->table_model . ".name as carModel");
Another option would be to put your object variables in braces {} such as :
$this->db->select("{$this->table_car}.id as carId,
{$this->table_car}.price as carPrice,
{$this->table_car}.name as carName,
{$this->table_car}.used as carUsed,
{$this->table_car}.visible as carVisible,
{$this->table_cat}.name as catName,
{$this->table_brand}.name as carBrand,
{$this->table_model}.name as carModel");
Related
I'm trying to construct a wp query that creates a string for each record:
$ttr_a = $wpdb->get_results("
SELECT CONCAT(
'"TR4":"',
t2.TaxAccount,
'", "',
CASE WHEN t1.TypeID = '1' THEN '"TR6":"Text A", '
WHEN t1.TypeID = '2' THEN '"TR6":"Text B", '
ELSE
CONCAT('"TR6":"', t1.NewID, '", ')
END
)
AS TID
FROM " . $wpdb->prefix . "table1 t1
LEFT JOIN " . $wpdb->prefix . "table2 t2
ON t1.NewID = t2.TaxCode
WHERE ID = " . $ID);
This query works fine in phpMyAdmin but the double quotes break the wpdb query. Is there some way to escape these? I've tried replacing with apostrophe and escaping the double qoutes with a double quote but nothing seems to work. If there are syntax errors here, please ignore them as I've just extracted what is needed, my sql query does work in mysql.
You should use a prepared statement here if at all possible. If that be not possible, then to escape literal double quotes inside a string defined with double quotes, try escaping the literal ones with backslashes:
$ttr_a = $wpdb->get_results("
SELECT CONCAT(
'\"TR4\":\"',
t2.TaxAccount,
'\", \"',
CASE WHEN t1.TypeID = '1' THEN '\"TR6\":\"Text A\", '
WHEN t1.TypeID = '2' THEN '\"TR6\":\"Text B\", '
ELSE
CONCAT('\"TR6\":\"', t1.NewID, '\", ')
END
)
AS TID
FROM " . $wpdb->prefix . "table1 t1
LEFT JOIN " . $wpdb->prefix . "table2 t2
ON t1.NewID = t2.TaxCode
WHERE ID = " . $ID);
How to merge column value in MySQL query? I'm using WordPress for a while now, and I would like to sort data from the price, but each post have different type price (monthly & yearly). The price is stored in wp_postmeta. Here is what I've tried so far.
$mam_global_fields = "
, price.meta_value AS villa_price,
CASE currency.meta_value
WHEN 'IDR' THEN price.meta_value * ". $currency_rate['IDR'] ." / 12
WHEN 'USD' THEN price.meta_value * ". $currency_rate['USD'] ." / 12
WHEN 'EUR' THEN price.meta_value * ". $currency_rate['EUR'] ." / 12
WHEN 'AUD' THEN price.meta_value * ". $currency_rate['AUD'] ." / 12
END AS price_in_usd,
CASE currency_monthly.meta_value
WHEN 'IDR' THEN price_monthly.meta_value * ". $currency_rate['IDR'] ."
WHEN 'USD' THEN price_monthly.meta_value * ". $currency_rate['USD'] ."
WHEN 'EUR' THEN price_monthly.meta_value * ". $currency_rate['EUR'] ."
WHEN 'AUD' THEN price_monthly.meta_value * ". $currency_rate['AUD'] ."
END AS price_in_usd_monthly,
price.meta_key AS price_key,
villa_code.meta_value AS v_codes
";
I join the wp_postmeta multiple times because I want to get the correct data for each post.
$mam_global_join = "
INNER JOIN " . $wpdb->postmeta . " AS currency_monthly ON (" . $wpdb->posts . ".ID = currency_monthly.post_id AND (currency_monthly.meta_key = 'monthly_currency'))
INNER JOIN " . $wpdb->postmeta . " AS price_monthly ON (" . $wpdb->posts . ".ID = price_monthly.post_id AND (price_monthly.meta_key = 'monthly_price'))
INNER JOIN " . $wpdb->postmeta . " AS currency ON (" . $wpdb->posts . ".ID = currency.post_id AND (currency.meta_key = 'currency'))
INNER JOIN " . $wpdb->postmeta . " AS price ON (" . $wpdb->posts . ".ID = price.post_id AND (price.meta_key = 'price'))
INNER JOIN " . $wpdb->postmeta . " AS villa_code ON (" . $wpdb->posts . ".ID = villa_code.post_id AND villa_code.meta_key = 'code')
";
For now it's ordering by 2 columns, but I want to only 1 column that's price_in_usd (but it still showing the wrong villas)
$mam_global_orderby = "price_in_usd, price_in_usd_monthly " . $sort[1];
So how to do this the correct way? because I want to make a sorting programs that will sort the price from both price (yearly or monthly), so if the post have yearly price then the price will be divided by 12. Please help.
I already tried using COALESCE() but I'm stuck, and those codes were the latest version.
EDIT:
Actually I had an idea, but don't know how to do it, I want to get the meta_value to a temporary price, so the yearly or monthly price will be in 1 column, I already looked in google but didn't getting the answer.
EDIT:
What I want, I want to merge the value from two different meta_key (monthly_currency & currency) to a single custom column, that I'll use for sorting, all price will be converted to USD before sorted it, so as you can see, there are CASE currency.meta_value in my code. So after knowing the currency, it'll check if the price if for monthly or yearly, if it's for yearly then it'll divided by 12. So I can sort it by the lowest/highest price, even if it's monthly or yearly rent. The problem I got is, I can't get the correct result, because the price type (yearly / monthly) so how to do it?
I would like to give extra parameter with join condition in hql.
In sql query I write
select a.*,ur.fname,ur.lname
from
atom as a
left join user as ur
on
a.id=ur.id
left join album as al
on
a.id=al.aid
and al.name='Profile'
left join post_images as pi
on
a.id=pi.aid
and pi.is_album_cover='yes'
where
(ur.fname like '%n%'
or ur.fname like '%n%')
and a.status='active';
which work propery and give desired result.
In hql I dont know how to give extra parameter with join
for fetching data I write(without extra parameter)
feeds = (List<Atom>) session.createQuery(
"select distinct atom from Atom as atom "
+ "left join fetch atom.albums as album "
+ "left join fetch album.postImageses as coverImage "
+ "left join fetch atom.user as user "
+ "where "
+ "(atom.user.fname like :name "
+ "or atom.user.lname like :name )"
+ "and album.name=:albumName "
+ "and coverImage.isAlbumCover=:isCover "
+ "and atom.status=:status ")
.setParameter("albumName", "Profile")
.setParameter("name", '%' + name + '%')
.setParameter("name", '%' + name + '%')
.setParameter("isCover", "yes")
.setParameter("status", "active")
.setFirstResult(0)
.setMaxResults(30)
.list();
Which is not giving any result how can I give extra parameter with join
In HQL, You may supply extra join conditions using the HQL with keyword.
Chech Associations and joins in HQL
Try this:
Your HQL::
select distinct atom
from Atom as atom
left join atom.albums as album with album.name= "Profile"
left join atom.postImageses as coverImage with coverImage.isAlbumCover="yes"
left join atom.user as user
where (user.fname like name or user.lname like name) and atom.status="active";
Your CODE::
feeds = (List<Atom>) session.createQuery(
"select distinct atom from Atom as atom "
+ "left join atom.albums as album with album.name=:albumName "
+ "left join atom.postImageses as coverImage with coverImage.isAlbumCover=:isCover "
+ "left join atom.user as user "
+ "where (user.fname like :name or user.lname like :name) "
+ "and atom.status=:status ")
.setParameter("albumName", "Profile")
.setParameter("name", '%' + name + '%')
.setParameter("isCover", "yes")
.setParameter("status", "active")
.setFirstResult(0)
.setMaxResults(30)
.list();
I think my question title is not clear enough so I place an example(symfony2 & Doctrine2):
$query = $this->_em->createQuery(
'SELECT j, jp, po, p from MjJobBundle:Job j'
. ' LEFT JOIN j.jobPackage jp'
. ' JOIN jp.packageOrder po'
. ' JOIN po.package p');
If I just want Job and Package entity I must retrieve all four entities is there any simple solution for that?
Do JOINS is the solutions. If you just want Job and Package entities, do not select the other entities:
$query = $this->_em->createQuery(
'SELECT j, p from MjJobBundle:Job j'
. ' LEFT JOIN j.jobPackage jp'
. ' JOIN jp.packageOrder po'
. ' JOIN po.package p');
I can't find a clear answer to this and my tests where inconclusive:
If I have a column in a table in a join that must be equal to (or in another relation with) a constant, is faster to put the condition in ON? Or at the end in WHERE?
Example:
SELECT * FROM `" . BLABLA . "` as `s`
JOIN `" . BLABLABLA . "` AS `sDet` ON (`sDet`.`a` > '" . $R['a'] . "'
AND '" . $R['b'] . "' BETWEEN `sDet`.`c` AND `sDet`.`d`
AND `s`.`id` = `sDet`.`idDet`
)
WHERE `s`.`f` = 'whatever'
Or
SELECT * FROM `" . BLABLA . "` as `s`
JOIN `" . BLABLABLA . "` AS `sDet` ON (`s`.`id` = `sDet`.`idDet`)
WHERE `s`.`f` = 'whatever'
AND '" . $R['b'] . "' BETWEEN `sDet`.`c` AND `sDet`.`d`
AND `s`.`id` = `sDet`.`idDet`
I was thinking first version should be faster but I'm not sure. Any thoughts?
I'm not quite sure whats quicker but do keep in mind that conditions are not always interchangeable between the where and on clause.
Inner join
In case of an INNER JOIN they are interchangable
Outer join
In case of an OUTER JOIN they are not necessarily interchangeable. It depends on which side of the join the conditions depends