Group Concat method in jquery data table - php

I have added the jquery data table and making the ajax request. On server side, I am using the joins of tables. The data table loads perfectly but search filter is not working properly. While searching ajax call shows that column "screen" is not found which is true as it does not exist in database. How can I solve that problem ?
$data = self::sql_exec( $db, $bindings,
"select r.title,group_concat(distinct n.name) as screens, group_concat(na.action) as permissions "."
FROM `$table` as r
left JOIN permissions p on p.role_id = r.id
left join `navigations` n on n.id = p.nav_id
left join `navigation_actions` na on na.id = p.action_id
$where
group by r.id
$order
$limit
"
);

One solution will be having another select statement on top of this statement.
Example (in mysql):
SELECT * FROM (
"select r.title,group_concat(distinct n.name) as screens, group_concat(na.action) as permissions "."
FROM `$table` as r
left JOIN permissions p on p.role_id = r.id
left join `navigations` n on n.id = p.nav_id
left join `navigation_actions` na on na.id = p.action_id
$where
group by r.id
$order
$limit
"
) AS my_table WHERE screens = "your_query_data"

Related

SQL query to fetch data of a particular user, from three tables

First is a user table, second a places table and third a favorites table (having two fk, one from user and another from places). I want to get all the favorite places of one user, how do I do that?
I tried to play around with joins but what I don't know is how to get data of a particular user.
$qry =
"SELECT places.place_id , places.db_image ,places.description from places
inner join favorites on places.place_id = favorites.place_id
inner join user on user.id = favorites.user_id
";
$query=mysqli_query($con ,$qry);
$return_arr = array();
$sql = sprintf(
'SELECT
p.place_id,
p.db_image,
p.description
FROM
places p INNER JOIN
favorites f ON p.place_id = f.place_id INNER JOIN
user u ON u.id = f.user_id
WHERE
u.id = %d'
, $userId);
or if you want simply select all columns from places
SELECT
p.*
FROM
places p INNER JOIN
favorites f ON p.place_id = f.place_id INNER JOIN
user u ON u.id = f.user_id
WHERE
u.id = %d
It is better to use prepared statements, but if you are not familiar with them, this is good enough.

I have been getting duplicate value from my inner join table

enter image description hereI have been using this sql code on my php. However, for every product i have been getting twice of them or a duplicate of them. How should i write to make it do not duplicate the product i get and just get once?
<?php
include ('classes/functions.php');
if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$check_receipt = "select si.shipping_name,
si.shipping_address,
si.shipping_contact,
si.shipping_email,
o.order_date,
o.trx_id,
o.tracking_num,
o.quantity,
o.store_id,
o.product_id,
p.product_title,
p.product_price,
p.product_img1,
p.product_weight
from shipping_infos si
inner join orders o
on si.user_id = o.user_id
inner join products p
on p.product_id = o.product_id
where si.user_id='".$user_id."' order by o.trx_id;" ;
$run_receipt_checking = mysqli_query($con, $check_receipt);
$result = array();
while($row = mysqli_fetch_array($run_receipt_checking)){
array_push($result,
array(
'shipping_name'=>$row[0],
'shipping_address'=>$row[1],
'shipping_contact'=>$row[2],
'shipping_email'=>$row[3],
'order_date'=>$row[4],
'trx_id'=>$row[5],
'tracking_num'=>$row[6],
'quantity'=>$row[7],
'store_id'=>$row[8],
'product_id'=>$row[9],
'product_title'=>$row[10],
'product_price'=>$row[11],
'product_img1'=>$row[12],
'product_weight'=>$row[13]
));
}
echo json_encode(array("result"=>$result));
}
?>
Duplicates in the result set imply multiple matches in the joins. Without sample data, it is really hard to tell where it is occurring.
If I had to guess based on the query, then would guess that users could have multiple rows in shipping_infos. There are definitely other possibilities, but I would start with a simple query and build up to the final query.
I'm guessing your p.product_id is an unique field in your DB.
If so you could add GROUP BY p.product_id at the end of your statement.
$check_receipt = "select si.shipping_name,
si.shipping_address,
si.shipping_contact,
si.shipping_email,
o.order_date,
o.trx_id,
o.tracking_num,
o.quantity,
o.store_id,
o.product_id,
p.product_title,
p.product_price,
p.product_img1,
p.product_weight
from shipping_infos si
inner join orders o
on si.user_id = o.user_id
inner join products p
on p.product_id = o.product_id
where si.user_id='".$user_id."' order by o.trx_id group by p.product_id;" ;

PHP SQL how the get the exact count from a table

I have two tables with the following columns:
FAMILY - id, ...
PRODUCTS - id, idfamily, type, ...
FAMILY & PRODUCTS are connected with family.id = products.idfamily.
I'm doing pagination over families with filters based on products types, so I'm trying to get the exact count of FAMILY containing almost one product with a specific type.
First query is ok, I get all the families:
if (!isset($_GET['type']) || $_GET['type'] == 'all') {
$query_family=mysql_query("SELECT * FROM family");
}
$count=mysql_num_rows($query_family);
// result = 166
Unfortunately, the following query is wrong:
} else {
$query_family=mysql_query("
SELECT * FROM family f LEFT JOIN products p ON f.id = p.idfamily
WHERE p.type = '$_GET[type]'
");
}
$count=mysql_num_rows($query_family);
// result = 500+
it's wrong because I get all the products with a type, but I'm trying to get the number of families containing products with the selected type ($_GET[type]).
Thank you
You should have
SELECT distinct f.id from family f LEFT JOIN products p on f.id = p.idfamily WHERE p.type = '$_GET[type]' in the second query
I think.
SELECT COUNT(*) as nbr FROM family
INNER JOIN products ON products.idfamily = family.id
WHERE product.type = ".intval($_GET['type'])."
GROUP BY family.id
Avoid the mysql_ driver, use PDO or mysqli instead. Don't forget to protect you from sql injections too.
To get the count of families with a specified type, use
SELECT * FROM family f INNER JOIN products p ON f.id = p.idfamily
WHERE IFNULL(p.type, 'notspecified') = '$_GET[type]'
With INNER JOIN instead of LFET JOIN you get only rows where a connection between f.id and p.idfamily exist. By LEFT JOIN, all family rows are returned with NULL values in the fields of product table.
So when p.type is NULL, your p.type = '$_GET[type]' evaluates always to NULL, and your filtering will not work as expected. For this reason, use IFNULL.
You can try below code:
$query_family=mysql_query("
SELECT COUNT(f.id) AS cnt FROM family f
INNER JOIN products p ON f.id = p.idfamily
WHERE p.type = '$_GET[type]'");
Also try to use mysqli not mysql.
And you don't have to use mysql_num_rows, you can get the right number directly from mysql:
$query = "SELECT COUNT(DISTINCT f.id) ".
"FROM family f ".
"LEFT JOIN products p ON f.id = p.idfamily ".
"WHERE p.type = '".$_GET['type']."'";
Use INNER JOIN instead of LEFT.

Trying to JOIN an empty table nothing returns

I have a problem trying to JOIN an empty table (comments table) to my existing prepared statement.
This is working perfectly:
// prepare images
if ($stmt = $mysqli->prepare(" SELECT uu.*, m.*,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID AND t.user_id = ?
) AS user_likes,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID
) AS total_likes
FROM user_uploads AS uu
INNER JOIN members AS m ON m.id = uu.user_id
ORDER BY up_time DESC")) {
$stmt->bind_param('i', $user_id);
$stmt->execute(); // get imgs
// foreach print images
// working as expected
}
And I don't know why if I JOIN another table (img_comments) that is empty, the images are not printed... if I add a row to the table and refresh the page, one image is printed...
The statement that I'm trying and it's not working is this:
SELECT uu.*, m.*, ic.*,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID AND t.user_id = ?
) AS user_likes,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID
) AS total_likes
FROM user_uploads AS uu
INNER JOIN members AS m ON m.id = uu.user_id
INNER JOIN img_comments AS ic ON ic.img_id = uu.imgID
ORDER BY up_time DESC
Why is only printing images based on the number of the table rows?? I also tried LEFT JOIN but I'm not too familiareize with this. I only use INNER JOIN in other scripts and I never had a problem like this.
I would appreciate any optimization to my query.
What does an inner join do? It joins all records of table a with all matching records of table b. So when there are no records in table b, there is no match for any record of table a, hence no result at all. Why does this surprise you?
A left join is an outer join (short for LEFT OUTER JOIN). It means: Give me all records of table a with all matching records of table b, and when there is no match then give me the record of table a anyhow. This seems to be what you are wanting here. But you say you tried it. I don't see how this would fail in your query.
A typical error for an outer join not to work would be to have some field of b in your where clause (e.g. where b.id > 100). As the outer-joined records have no matching b record, all b fields are null, so that such a where clause would fail. You'd just get matches again, just like with the inner join.
EDIT: As to optimization, you can get the two counts in one pass by counting conditionally:
SELECT
uu.*, m.*, ic.*,
il.count_user AS user_likes,
il.count_total AS total_likes
FROM user_uploads AS uu
INNER JOIN members AS m ON m.id = uu.user_id
LEFT OUTER JOIN img_comments AS ic ON ic.img_id = uu.imgID
LEFT OUTER JOIN
(
select
img_id,
count(*) as count_total,
count(case when t.user_id = ? then 1 end) as count_user
from img_likes
group by img_id
) AS il ON il.img_id = uu.imgID
ORDER BY uu.up_time DESC;
As far as I know, INNER JOIN will only retrieve data which have both data. So if let say the table that you join have no data with that join condition. It will not return any data at all.
LEFT JOIN is just a normal join. It will retrieve data on both table. But if the joined table is empty, then only the primary table will have data, the secondary table will have null as its data.
You can just modify your code, replacing INNER JOIN with LEFT JOIN and see if it works/

datatable into cakephp join tables

I have problem with datatables and script from
http://datatables.net/development/server-side/php_cake to cakephp.
I using it many time but no I have to use join and server response me
MySQL Error: 1052.
Problem is in this line :
$sTable = "`clients` AS c JOIN users AS u ON (c.user_id = u.id)";
When I print my all query and paste it in phpmyadmin everything is OK, but cake get error.
My generated SELECT :
SELECT SQL_CALC_FOUND_ROWS c.id, c.name, c.nip, c.adress, c.tel,
c.tel2, c.email, c.created, c.id
FROM clients c join users u ON(c.user_id = u.id)
ORDER BY c.id asc
LIMIT 0, 10
#edit
I modificated my script and now it generate something like this:
SELECT SQL_CALC_FOUND_ROWS
c.id,c.name,c.nip,c.adress,c.tel,c.tel2,c.email,c.created,c.id
FROM clients AS c join users AS u ON c.user_id = u.id
ORDER BY `c`.`name` asc
LIMIT 0, 10
But it's still not work. (I used char '`' before prefix and after and col name. )
you need create a view with the relationship!!
Create the VIEW in MySql:
CREATE VIEW 'the_view' AS
SELECT
a.id,
a.num_factura_proveedor,
b.nombre_comercial
FROM compras a
INNER JOIN terceros b ON a.tercero_id = b.id
on the server side script:
$sTable = "the_view";
//the columns of the view
$aColumns = array('id' , 'num_factura_proveedor', 'nombre_comercial');
$sIndexColumn = "id";

Categories