Use select filed result on LEFTJOIN string - mysql

I'm trying to use id_program result from the availability table inside a string in LEFTJOIN, is it possible?
I tried using this {post.id_program} but its not working.
SELECT *
FROM availability post
LEFT JOIN postmeta meta5 ON meta5.post_id = post.id_post
AND meta5.meta_key = 'items_iti_port_{post.id_program}_nights_iti_port'
WHERE post.id_post=462
EDIT:
http://sqlfiddle.com/#!9/aeb4a7/3

I see two ways..
Using CONCAT():
meta5.meta_key = CONCAT('items_iti_port_', post.id_program, '_nights_iti_port')
With REPLACE():
meta5.meta_key = REPLACE('items_iti_port_%_nights_iti_port', '%', post.id_program)

Related

Codeigniter INNER JOIN with multiple ON [duplicate]

I want to select data from my database table with join query, but my it doesn't work.
My query:
$this->db->select();
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->join('schedule', 'schedule.itemtype = 'testitem'');
$this->db->where('we.isActive','Y');
This line makes problem with schedule.itemtype = 'testitem':
$this->db->join('schedule', 'schedule.itemtype = 'testitem'');
How can I solve this?
You don't need to join same table twice.
But just to extend ON clause:
$this->db->select();
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid AND schedule.itemtype = \'testitem\'');
$this->db->where('we.isActive','Y');
try
$this->db->select();
$this->db->from("we");
$this->db->join("schedule", "schedule.itemid = we.cid");
$this->db->where("schedule.itemtype","testitem");
$this->db->where("we.isActive","Y");
I believe there are two problems here. The first problem is that you are using one too many quotes in the second join line in your query:
You have: $this->db->join('schedule', 'schedule.itemtype='testitem''); < extra quote
It should be: $this->db->join('schedule', 'schedule.itemtype=testitem');
Second problem: your join doesnt make sense.
Your statement:
$this->db->select();
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->join('schedule', 'schedule.itemtype = testitem');
$this->db->where('we.isActive','Y');
Translates to:
SELECT * FROM we
JOIN schedule ON schedule.itemid = we.cid
JOIN schedule ON schedule.itemtype = testitem
WHERE we.isActive = Y
As you can see you are joining the same table twice on different lines, not only that but what table does "testitem" belong to? We are left to assume that you perhaps want the join where itemtype = testitem which will mean this:
SELECT * FROM we
JOIN schedule ON schedule.itemid = we.cid
WHERE schedule.itemtype = testitem
AND we.isActive = Y
Therefore your final Codeigniter query should be:
$this->db->select('*');
$this->db->from('we');
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->where('schedule.itemtype', 'testitem');
$this->db->where('we.isActive','Y');
This will work:
$this->db->join('schedule', 'schedule.itemid = we.cid');
$this->db->where('we.isActive','Y');
$this->db->where('schedule.itemtype', 'testitem');
$this->db->get('we');
$this->db->query('select we_tbl.c_name from we we_tbl,schedule sch_tbl where sch_tbl.itemid = we_tbl.cid AND we_tbl.idActive = '.$activeData);
Try this query according to your problem this could get the data you need.
I've tested on different database but i tried to perform what you're trying to get. https://www.w3schools.com/sql/trysql.asp?filename=trysql_op_in
select
pro_tbl.ProductName,
cat_tbl.CategoryName ,
sup_tbl.SupplierName
from
Products pro_tbl,
Suppliers sup_tbl,
Categories cat_tbl
where
pro_tbl.SupplierID = sup_tbl.SupplierID AND
pro_tbl.CategoryID = cat_tbl.CategoryID;
Two possible problems, depending on what your desired outcome is:
If you need to make two joins and are getting an error with the second join clause, try using double quotes to enclose the constant value on the condition or you'll get a parse error:
$this->db->join('schedule', 'schedule.itemtype = "testitem"');
If you need to join only once with multiple conditions, use parentheses:
$this->db->select('*');
$this->db->from('we');
$this->db->join('schedule', '(schedule.itemid = we.cid AND schedule.itemtype="testitem")');
$this->db->where('we.isActive','Y');
You query is equivalent to writing:
select * from we
inner join schedule on schedule.itemid = we.cid
inner join schedule on schedule.itemtype = "testitem"
where we.isActive = 'Y'
but what you seem to need is
select * from we
inner join schedule on (schedule.itemid = we.cid AND schedule.itemtype = "testitem")
where we.isActive = 'Y'
On your original query, you are doing two joins. In the latter, you'll do only one with multiple conditions.

How to search exact string using MySql Query which avoids extended values?

My Query looks like
$search_query = db_query("SELECT nd.nid, users.name, nd.type FROM node as nd
LEFT JOIN node_revisions as nd_rev ON nd_rev.nid = nd.nid AND nd_rev.vid = nd.vid
LEFT JOIN users ON nd.uid = users.uid
WHERE nd.status = 1 AND nd_rev.body LIKE LOWER('%node/100%')
AND nd.nid NOT IN(SELECT DISTINCT nid FROM term_node WHERE tid = 293)");
This query actually returns all the matches from node_revisions.body field, Which includes
node/1000, node/1001.... Etc.,
I want to get only the result of exact match where possible like
"node/100"
"node/100/"
"/node/100"
"/node/100/"
'node/100'
'node/100/'
'/node/100'
'/node/100/'
and not like
"node/1006"
"node/10064/"
"/node/1000"
"/node/10001/"
'node/10023'
'node/1005/'
'/node/1001'
'/node/10069/'
This above query returned me result which has string like below..
..a href="/node/1006"
How to avoid this kind of errors? Please help..
Try removing the % after 100 so the search won't consider any digit after 100, like this:
LOWER('%node/100')
Then consider the following Regular Expression
Example:
`nd_rev.body` REGEXP "^/?node/100/?$"
Oh ya... I got an resolution for this.. I redefined my query like below and it gives me result as expected..
$search_query = db_query("SELECT nd.nid, users.name, nd.type FROM node as nd
LEFT JOIN node_revisions as nd_rev ON nd_rev.nid = nd.nid AND nd_rev.vid = nd.vid
LEFT JOIN users ON nd.uid = users.uid
WHERE nd.status = 1 AND nd_rev.body RLIKE '[[:<:]]" . $search_string . "[[:>:]]'
AND nd.nid NOT IN(SELECT DISTINCT nid FROM term_node WHERE tid = 293)");
Look at
nd_rev.body RLIKE '[[:<:]]" . $search_string . "[[:>:]]'
This is what i expected

mysql parameterized query in ASP.NET

i am working on parameterized queries but i am not getting proper query in result
here is my code
public MySqlCommand Get_Login(string clinetID, string loginID, string password, string branchID)
{
MySqlCommand objCommand = new MySqlCommand(this.Query);
objCommand.Parameters.AddWithValue("#ClientID", clinetID);
objCommand.Parameters.AddWithValue("#LoginID", loginID);
objCommand.Parameters.AddWithValue("#Password", password);
objCommand.Parameters.AddWithValue("#BranchID", branchID);
objCommand.CommandType = CommandType.Text;
return objCommand;
}
and when debugging this is what i am getting in "objCommand"
Select u.groupid,p.PersonId, p.designationid,concat(p.salutation,p.FName,'
',p.MName,' ',p.LName) as PersonName,tb.Type
BrType,p.OrgId,p.subdepartmentid,ifnull(crossdept,'N') as
crossdept,p.departmentid,u.defaultpage,p.orgid,ifnull(p.crosslab,'N') as crosslab,
(select indoor_services from dc_Tp_organization where orgid='#ClientID') as
indoor_services,(select name from dc_Tp_organization where orgid='#ClientID') as
orgname,
(select default_route from dc_Tp_organization where orgid='#ClientID') as
default_route,p.BranchID BranchID,tb.Name BRName from dc_tp_personnel p left outer
join
dc_tu_userright u on u.personid=p.personid left outer join dc_tp_branch tb on
tb.BranchID=p.BranchID Where p.Active='Y' and p.LoginId = '#LoginID' and p.Pasword
='#Password' and p.BranchID='#BranchID'
i am not getting values in parameters
Here is the Query
objdbhims.Query = "Select u.groupid,p.PersonId,
p.designationid,concat(p.salutation,p.FName,' ',p.MName,' ',p.LName) as
PersonName,tb.Type BrType,p.OrgId,p.subdepartmentid,ifnull(crossdept,'N') as
crossdept,p.departmentid,u.defaultpage,p.orgid,ifnull(p.crosslab,'N') as crosslab,
(select indoor_services from dc_Tp_organization where orgid=#ClientID) as
indoor_services,(select name from dc_Tp_organization where orgid=#ClientID) as
orgname,(select default_route from dc_Tp_organization where orgid=#ClientID) as
default_route,p.BranchID BranchID,tb.Name BRName from dc_tp_personnel p left outer
join dc_tu_userright u on u.personid=p.personid left outer join dc_tp_branch tb on
tb.BranchID=p.BranchID Where p.Active='Y' and p.LoginId = #LoginID and p.Pasword
=#Password and p.BranchID=#BranchID";
Secret Squirrel was correct on using the "?" for parameterized variables. MySQL uses "#" for inline sql variables for queries and thus expecting them to be declared such as from a script or part of an inline (select subquery) declaration.
You need to change BOTH instances of the parameters... both in the query, and as the command.Parameters.Add... instances.
Also, I noticed, and not sure if its it or not, but in your WHERE clause you have "pasword" (only one 's') vs password (two 's') Don't know if intentional or not.
One LAST thing that MAY help. Since some of the parameters match the column names, I would suggest changing the parameters SLIGHTLY by just adding something like "x" to FORCE differentiation between the column name and the actual parameters...
where... p.LoginID = ?xLoginID ...
and in the command parameters
objCommand.Parameters.AddWithValue("?xLoginID", loginID);
The problem is because the parameters were wrap with single quotes converting them into string literals.
To make it work, remove the single quotes around them. eg.
Where p.Active = 'Y'
and p.LoginId = #LoginID
and p.Pasword = #Password
and p.BranchID = #BranchID

remove zero in mysql field using join

code = 00000000005555
2nd option code = 00000000000555
hi i am try to find out same function like ltrim() of php
SELECT * FROM db1.stock JOIN db2.prodinfo ON replace(db2.prodinfo.code,0000000000,'') = replace(db1.stock.code,0000000000,'') WHERE db1.stock.InvNo ='12' and db2.prodinfo.Cat = 'super'
i rum this temporary. becuse zero might be increase and decrease i
example above. i am just want to remove zero in this query
thanks
try by casting it into numeric,
SELECT *
FROM db1.stock JOIN db2.prodinfo ON
CAST(db2.prodinfo.code AS SIGNED) = CAST(db1.stock.code AS SIGNED)
WHERE db1.stock.InvNo ='12' and db2.prodinfo.Cat = 'super'
or
SELECT *
FROM db1.stock JOIN db2.prodinfo ON
CAST(db2.prodinfo.code AS DECIMAL(15,0)) = CAST(db1.stock.code AS AS DECIMAL(15,0))
WHERE db1.stock.InvNo ='12' and db2.prodinfo.Cat = 'super'

Assistance with SELECT

I have three tables: hosting_pack, hosting_attr and hosting_attr_value.
One hosting_category can have many hosting_packs and many hosting_attributes.
To know the hosting_value, we must know the hosting_pack and the hosting_attribute, so hosting_pack contains the pack information, hosting_attr contains the attributes, and hosting_attr_value contains the values of every attribute in one pack.
Described as follows :
I want to display a table like this:
hosting_attr, hosting_attr_value , for a given id_hosting_pack
Note: the relation between hosting_categ and hosting_attr doesn't exist, just ignore it.
Try this:
select hosting_attr.attr_label, hosting_attr_value.hosting_attr_value from hosting_attr_value, hosting_attr where hosting_attr_value.id_hosting_pack = ? and hosting_attr.id_hosting_attr = hosting_attr_value.id_hosting_attr
If you do not know id_hosting_pack, you can look it up in-line
select hosting_attr.attr_label, hosting_attr_value.hosting_attr_value from hosting_attr_value, hosting_attr where hosting_attr_value.id_hosting_pack = (select id_hosting_pack from hosting_pack where pack_name = ?) and hosting_attr.id_hosting_attr = hosting_attr_value.id_hosting_attr
select ha.attr_label, hav.attr_value from hosting_attr ha
inner join hosting_attr_value hav on ha.id_hosting_attr = hav.id_hosting_attr
where hav.id_hosting_pack = :theIdOfTheHostingPack
Try:
select *
from
hosting_pack hp,
hosting_attr_value hav,
hosting_attr ha
where
hp.id_hosting_pack = hav.id_hosting_pack
and hav.id_hosting_attr = ha.id_hosting_attr
Was that it?
And also try to study SQL a little bit.