Convert sql query to codeiginiter - php

SELECT *
FROM `tb_slots`
WHERE `service_name`='Paragliding'
and repetition > (
SELECT count(*)
FROM `user_booking`
WHERE date='2019-11-17'
and `time-session`=`tb_slots`.`id`
and `pay_status`='Success'
);

There is no such thing as converting a SQL query to CodeIgniter.
If you wish to have the query working in CodeIgniter's query builder, it's should be easy following the documentation here:
https://codeigniter.com/user_guide/database/query_builder.html

I hope this would help you
$this->db->select('*');
$this->db->where('service_name','Paragliding');
$this->db->where('`repetition` > (SELECT count(*) FROM user_booking WHERE date="2019-11-17" and time-session=tb_slots.id and pay_status="Success"'));
$this->db->get('tb_slots');
thanks

Related

What is the best way to handle this sub query in laravel using DB:raw?

After several attempts I am having trouble getting this raw query formatted using DB::raw in laravel. Could someone point me in the right direction? I am trying to avoid using DB::table or spliting it up into multiple queries if possible.
select id
from ParticipantDetail
where testId= (
select testId from PacketDetail where PacketDetail.packetid=ParticipantDetail.packetid and [order]='1'
)
and packetid = (
select packetid from ParticipantDetail where id = '1f4716e9-6e8b-41ce-b746-60a013fab38f'
)
and masterid = (
select masterid from ParticipantDetail where id = '1f4716e9-6e8b-41ce-b746-60a013fab38f'
)
I think there is no need to make these sub-queries
I changed the query a little bit. and used DB::select() to run it.
DB::select("select partDtl.id from ParticipantDetail partDtl
INNER JOIN PacketDetail packDtl on packDtl.packetid = partDtl.packetid
WHERE partDtl.id = ? and packDtl.order = ?",
['1f4716e9-6e8b-41ce-b746-60a013fab38f', '1']);
I hope it will work.

Laravel query with temporary table

How to convert this query to laravel db query.
SELECT * FROM {
Select * from organizers
Order by organizers.rank
} Group by t.department
This is simplified version of query. In real the inner query has more where clause and built using laravel db query.
Edit: I am aware of raw query. But that's not what I am looking for. Inner query is complex and has lots of conditional where clause. I would like to retain the db query object I used there.
You can have 2 different query builders and merge their binding like below :
$innerQuery = DB::table('organizers')->orderBy('organizers.rank');
$mainQuery = DB::table(DB::raw('(' . $innerQuery->toSql() . ') as t'))
->mergeBindings($innerQuery->getQuery())
->groupBy('t.department')
->get();
This will also help you retail the $innerQuery builder instance for your later use as you have mentioned in the question.
I think you will have to execute a raw query.
$result = DB::select("SELECT * FROM (
Select * from organizers
Order by organizers.rank
) Group by t.department");
reference: https://laravel.com/docs/5.7/queries#raw-expressions

How to convert mysql query to codeigniter using query builder?

The following is my mysql query:
select * from db_posts LEFT JOIN db_followers ON db_posts_user_id = db_followers_following AND db_followers_user_id = 276
How can I convert it to codeigniter using query builder?
$this->db->where('db_posts_user_id', 'db_followers_following');
$this->db->where('db_followers_user_id', 276);
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id');
$query2 = $this->db->get('db_posts')->result_array();
I get the required result when I use mysql query. But I get a blank array when I use codeigniter queries. What is wrong in my CI Query?
why are you using those where clause? if you are using it as condition for your 'JOIN' process, try it like this:
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id and db_followers_user_id = 276', 'left');
$query2 = $this->db->get('db_posts')->result_array();
Points to Note
1)as previous comments said, you should 'Left Join' if you want to get all posts from 'db_posts'.
2) You can add Multiple conditions in ON Clause using and but within ''. all your conditions should be specified before second comma(,) which is before mentioning 'LEFT'.
Hope this will help. Lemme know if this help
Try adding the "Left" option to tell it what kind of join to do:
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id', 'Left');
Also I'm not sure you need
$this->db->where('db_posts_user_id', 'db_followers_following');
this is already covered by your JOIN statement.
Source:
http://www.bsourcecode.com/codeigniter/codeigniter-join-query/#left-join and https://www.codeigniter.com/userguide3/database/query_builder.html

how to write a mysql function in codeigniter join() method

I'm in big trouble in codeigniter.
I want to use FIND_IN_SET mysql function in codeigniter join() function. But problem is codeigniter consider FIND_IN_SET as a field name.
Please check below code:
$this->db->select("gcpo.promotional_offer_id,gcpo.promotional_offer_name,gcpo.promotional_offer_code,gcpo.promotional_offer_type,gcpo.promotional_offer_discount,gcpo.promotional_offer_min_amount,gcpo.promotional_offer_uses_per_offer,gcpo.promotional_offer_start_date,gcpo.promotional_offer_end_date,name,gcpo.promotional_offer_is_active,gcpo.promotional_offer_added_date,count(gcopo.promotional_offer_code) as cntP");
$this->db->from("promotional_offer gcpo");
$this->db->join("customer_groups", "FIND_IN_SET(id,promotional_offer_customer_group) > 0");
$this->db->join("order_promotional_offer gcopo", "gcopo.promotional_offer_code=gcpo.promotional_offer_code","left");
$this->db->group_by('gcpo.promotional_offer_code');
$this->db->limit($_GET['iDisplayLength'], $start);
$this->db->order_by($sort_array[$_GET['iSortCol_0']], $_GET['sSortDir_0']);
$query = $this->db->get();
In mysql query output which given by codeigniter:
SELECT `gcpo`.`promotional_offer_id`, `gcpo`.`promotional_offer_name`, `gcpo`.`promotional_offer_code`, `gcpo`.`promotional_offer_type`, `gcpo`.`promotional_offer_discount`, `gcpo`.`promotional_offer_min_amount`, `gcpo`.`promotional_offer_uses_per_offer`, `gcpo`.`promotional_offer_start_date`, `gcpo`.`promotional_offer_end_date`, `name`, `gcpo`.`promotional_offer_is_active`, `gcpo`.`promotional_offer_added_date`, count(gcopo.promotional_offer_code) as cntP FROM (`gc_promotional_offer` gcpo) JOIN `gc_customer_groups` ON `FIND_IN_SET`(`id,promotional_offer_customer_group)` > 0 LEFT JOIN `gc_order_promotional_offer` gcopo ON `gcopo`.`promotional_offer_code`=`gcpo`.`promotional_offer_code` GROUP BY `gcpo`.`promotional_offer_code` ORDER BY `gcpo`.`promotional_offer_added_date` desc LIMIT 10
now please find the find_in_set function in mysql query you will find like field name that consider by codeigniter .
FIND_IN_SET is a restriction function, you want to use it with a where. First you need to join customer_groups, and then restrict results with a where.
Change __PUT JOIN CONDITION HERE__ with your condition, and prefix FIND_IN_SET with correct table alias.
$this->db->select("gcpo.promotional_offer_id,gcpo.promotional_offer_name,gcpo.promotional_offer_code,gcpo.promotional_offer_type,gcpo.promotional_offer_discount,gcpo.promotional_offer_min_amount,gcpo.promotional_offer_uses_per_offer,gcpo.promotional_offer_start_date,gcpo.promotional_offer_end_date,name,gcpo.promotional_offer_is_active,gcpo.promotional_offer_added_date,count(gcopo.promotional_offer_code) as cntP");
$this->db->from("promotional_offer gcpo");
$this->db->join("order_promotional_offer gcopo", "gcopo.promotional_offer_code=gcpo.promotional_offer_code","left");
$this->db->join("customer_groups", "__PUT JOIN CONDITION HERE__");
$this->db->where("FIND_IN_SET(id,promotional_offer_customer_group) > 0");
$this->db->group_by('gcpo.promotional_offer_code');
$this->db->limit($_GET['iDisplayLength'], $start);
$this->db->order_by($sort_array[$_GET['iSortCol_0']], $_GET['sSortDir_0']);
$query = $this->db->get();

Query with database link server with pdo in php

I'm facing a problem right now, i explain :
I have my SQL query :
SELECT COUNT(*) AS nb, TO_CHAR(myDate,'yyyy-mm-dd') AS dateF, T1.IDCar, T2.IDMotor
FROM TDB2#MYLINKSERVER,T1
JOIN T2 ON T1.ID = T2.ID
WHERE TDB2.IDCar = T1.IDCar
Which use a TDB2#MYLINKSERVER , to connect 2 differents databases from Oracle and use a table from and other database.
In fact, when i launch the query in sql developper, my query returns some datas.
So in php, i have
$sql = $pdo->prepare('myquerybefore')
$sql->execute(array($annee));
$res = $sql->fetchAll(PDO::FETCH_ASSOC);
And when i try to vardump my $res, it returns array(0) { }
So i think that PDO don't use the #LINKSERVER to fetch results.
Any help would be appreciated
Sleakerz,
I found a way to make my query work :
I create a synonym with
CREATE PUBLIC SYNONYM LINKDATABASE
FOR TABLE1#THELINKDATABASE;
Then i have in my query: FROM LINKDATABASE which is my synonym !

Categories