how can I display like result of query in phpmyadmin? - codeigniter

Model
$q2 = "SELECT a.id, name, price FROM product a inner join detail b
on a.id = b.id
WHERE a.id IN ('".$id."') and status = 1 ";
$result = $this->db->query($q2);
return $result->result_array();
where $id = 41001,41002
so, i try it into phpmyadmin. here's the result
phpmyadmin
SELECT a.id, name, priceFROM product a inner join detail b
on a.id = b.id
WHERE a.id IN (41001,41002) and status = 1
the result of that query are :
id name price
41001 Coca Cola 4500
41002 Pepsi 4500
View
<?php foreach($resultsearch as $r){?>
<li class="span3">
<div class="product-box">
<?php echo $r['id']?>
<?php echo $r['name']?><span><br/>
<span>Rp. <?php echo number_format($r['price'],2,",",".")?></span><br/>
</div>
</li>
<?php $no++;}?>
but, only display
41001 Coca Cola 4500
how can I display like result of query in phpmyadmin?

try your query like this
$q2 = "SELECT a.id, name, price FROM product a inner join detail b
on a.id = b.id
WHERE a.id IN (".$id.") and status = 1 ";
$result = $this->db->query($q2);
return $result->result_array();

try
echo $result->num_rows();
If the result is 2, then you have a problem in the controller, you must have something like this:
$this->load->view('myview', Array('resultsearch' => $this->MYMODEL->myquerymethod($id)));

Related

how to write query with "with" using laravel eloquent

How to make with laravel a query with with like the example below
Thank you
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
WITH clauses are not supported by Eloquent. You can either pass in a raw query
$sql = <<<SQL
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
SQL;
$results = DB::select($sql);
Or use a package that adds this functionality like staudenmeir/laravel-cte
DB::query()
->withExpression('s', fn ($query) => $query->select('s.id')->from('s'))
->select('a.*')
->from('a')
->leftJoin('s', 's.id', 'a.s_id')
...

convert subquery to use Cases and Join

I have a query which is working fine. I want to convert it to use Cases and Join.
SELECT id, Ticket_id, serial_no, operator,
(SELECT value FROM Forms_Details where FORM_ID = cv.id and status = 'ACTIVE' and key = 'COMPUTER_TYPE') as COMPUTER_TYPE,
(SELECT value FROM Forms_Details where FORM_ID = cv.id and status = 'ACTIVE' and key = 'BRAND') as BRAND,
(SELECT value FROM Forms_Details where FORM_ID = cv.id and status = 'ACTIVE' and key = 'MODEL') as MODEL,
status, datetime
FROM Form cv
where status = 'ACTIVE' order by id desc;
If you want to join, then join. What keeps you from doing so?
SELECT
f.id,
f.Ticket_id,
f.serial_no,
f.operator,
ct.value AS computer_type,
b.value AS brand,
m.value AS model,
f.status,
f.datetime
FROM form f
LEFT JOIN forms_details ct ON ct.form_id = f.id AND ct.status = 'ACTIVE' AND ct.key = 'COMPUTER_TYPE'
LEFT JOIN forms_detailswhere b ON b.form_id = f.id AND b.status = 'ACTIVE' AND b.key = 'BRAND'
LEFT JOIN forms_detailswhere m ON m.form_id = f.id AND m.status = 'ACTIVE' AND m.key = 'MODEL'
WHERE f.status = 'ACTIVE'
ORDER BY f.id DESC;
Or, in order not to read the same table twice, you can use conditional aggregation to get the values from the table:
SELECT
f.id,
f.Ticket_id,
f.serial_no,
f.operator,
ct.value AS computer_type,
fdw.brand,
fdw.model,
f.status,
f.datetime
FROM form f
LEFT JOIN forms_details ct ON ct.form_id = f.id AND ct.status = 'ACTIVE' AND ct.key = 'COMPUTER_TYPE'
LEFT JOIN
(
SELECT
form_id,
MAX(CASE WHEN key = 'BRAND' THEN value END) AS brand,
MAX(CASE WHEN key = 'MODEL' THEN value END) AS model
FROM forms_detailswhere
WHERE status = 'ACTIVE' AND key IN ('BRAND', 'MODEL')
GROUP BY form_id
) fdw ON fdw.form_id = f.id
WHERE f.status = 'ACTIVE'
ORDER BY f.id DESC;
You can PIVOT and then join:
SELECT cv.id,
cv.Ticket_id,
cv.serial_no,
cv.operator,
fd.computer_type,
fd.brand,
fd.model,
cv.status,
cv.datetime
FROM Form cv
LEFT OUTER JOIN (
SELECT *
FROM (
SELECT form_id, key, value
FROM Form_Details
WHERE status = 'ACTIVE'
)
PIVOT (
MAX(value)
FOR key IN (
'BRAND' AS brand,
'MODEL' AS model,
'COMPUTER_TYPE' AS computer_type
)
)
) fd
ON (cv.id = fd.form_id)
where cv.status = 'ACTIVE'
order by id desc;

laravel query to make pagination

$users = SELECT users.id,
users.name,
cd.title,
cd.updated_at,
profiles.avatar
FROM `users`
JOIN (SELECT Max(id) max_id,
user_id
FROM book
GROUP BY user_id) c_max
ON ( c_max.user_id = users.id )
JOIN book cd
ON ( cd.id = c_max.user_id )
JOIN profiles
ON ( profiles.user_id = users.id )
ORDER BY cd.updated_at DESC
i am using this to get data in laravel query
$users = DB::select(DB::raw('SELECT users.id, users.name, cd.title, cd.updated_at, profiles.avatar FROM users JOIN ( SELECT MAX(id) max_id, user_id FROM book GROUP BY user_id )
c_max ON (c_max.user_id = users.id) JOIN book cd ON (cd.id = c_max.user_id) JOIN profiles ON (profiles.user_id = users.id) ORDER by cd.updated_at DESC limit 24 offset 0'));

How to translate SQL to LINQ?

sql
select * from (select * from tabla limit 1) as a inner join table b on a.id = b.id where a.id = id
LINQ
from s in tableA
join c in (from a in tableB where a.id==id select a).FirstOrDefault() on s.id equals c.id
where s.id == id
select s
I want to translate this sql to linq but failed. How can I do translate?
I think this will do it, but you should check what sql is being generated by linq.
a = tableA.FirstOrDefault();
//First option
tableB.Where(xb => xb.id = a.id)
.Select(xb => new {a = a, b = xb})
.Where(abx => abx.a.id == id)
//Second option
tableB.Select(xb => new {a = a, b = xb})
.Where(abx => abx.a.id == id && abx.a.id = abx.b.id)

Codeigniter 2.2 pagination not showing

Try for hour but not working. I can manually switching the /welcome/index/1 or 2 or 3 in the URL. Just the damn pagination < 1 2 3 4 > not showing !
My controller
$offset = $this->uri->segment(3,0);
$query = $this->db->query(
'SELECT p.id AS pid,
p.url,
p.created_time,
t.name,
t.num_photo,
t.id AS tid
FROM photos p
LEFT JOIN tag_maps AS tm ON p.id = tm.photo_id
LEFT JOIN tags AS t ON t.id = tm.tag_id
INNER JOIN
(
SELECT MAX(created_time) maxDate, t.id
FROM photos p
LEFT JOIN tag_maps AS tm ON p.id = tm.photo_id
LEFT JOIN tags AS t ON t.id = tm.tag_id
GROUP BY t.id
) AS d
ON p.created_time = d.maxDate
AND t.id = d.id
ORDER BY p.created_time LIMIT ' . $offset . ',2'
);
$config['base_url'] = 'http://localhost:8080/example/welcome/index';
$config['total_rows'] = $query->num_rows();
$config['per_page'] = 2;
$config['num_links'] = 20;
Then in my html
<?php echo $this->pagination->create_links(); ?>
The problem is lied on this
$config['total_rows'] = $query->num_rows();
You can't use the $query->num_rows();, as this is always return 2 rows.
You should have another query to get the total rows for the actual query,
which should be a SELECT COUNT(*)

Resources