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";
Related
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"
I have Quiz table which is shown in image.
and quiz result history table like
I write left join query between these two table.
My query is like:
select q.*
, c.name as catname
, qrh.q_complete
, q s.test_type
, qs.questionid
, sum(qrh.score) as score1
, sum(qrh.total_questions) as tot_que
from categories c
, quiz q
left
join user_quiz_results_history qrh
on qrh.quiz_id = q.quizid
left
join quizquestions qq
on qq.quizid = q.quizid
left
join questions qs
on qs.subcatid = qq.subjectid
where c.catid = q.catid
AND q.catid = 1
AND q.status = 'Active'
AND q.enddate >= '2016-05-02'
group
by q.quizid
This query give result all quiz table data and related user_quiz_results table.
If I write query using condition for left join table like userid=90, I don't get the any of data.
I want all data of quiz and if table have result for userid=90 get data from table user_quiz_history.avg(score).
Write condition on left join clause.
select q.* , qrh.q_complete , sum(qrh.score) as score1 , sum(qrh.total_questions) as tot_que from categories c , quiz q left join user_quiz_results_history qrh on qrh.quiz_id = q.quizid AND qrh.userid=84 where q.catid = 1 AND q.status = 'Active' AND q.enddate >= '2016-05-02' group by q.quizid
I am applying Left Join with Sub query & where clause
It seems fine no syntax error but the columns I am selecting from that sub query always returns me Null. I have executed the same part in my SQL section, it gives me record. Kindly have a look on the query and let me know if any thing is possible or if question is not clear.
SELECT alt.userId, u.name, t.name AS teamName, alt.startDateTime, v.name AS villageName, c.name AS clusterName, startLat, startLong, latlng.lat, latlng.long
FROM activity_log_tim AS alt
JOIN user AS u ON u.userId = alt.userId
JOIN team_members AS tm ON tm.memberId = u.userId
JOIN team AS t ON t.teamId = tm.teamId
JOIN village AS v ON v.villageId = alt.villageId
JOIN cluster_villages AS cv ON cv.villageId = v.villageId
JOIN cluster AS c ON c.clusterId = cv.clusterId
LEFT JOIN (SELECT lat, long,dateTime, scheduleId FROM activity_log_gps LIMIT 1) AS latlng ON latlng.scheduleId = alt.scheduleId
WHERE DATE(alt.startDateTime) = '2015-09-05' AND DATE(alt.endDateTime) = '0000-00-00' GROUP BY alt.userId ORDER BY latlng.dateTime DESC
Well it's supposed to since you are performing a LEFT JOIN which produces NULL if no match occurs. So either you can perform a INNER JOIN or use a COALESCE function like below
SELECT alt.userId, u.name,
t.name AS teamName,
alt.startDateTime,
v.name AS
villageName,
c.name AS clusterName,
startLat,
startLong,
COALESCE(latlng.lat, 23), //Notice the use of COALESCE. If null will return 23 as default
COALESCE(latlng.long, 32)
I was using this:
SELECT res.*, rac.*, u.*, t.*, c.*
FROM Results res
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
INNER JOIN Cars c USING (CarID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
Which works fine, but I've since realised I need to have CarID added in to Results table, but when I add it in, it gives me the error that the field is ambiguous. What I'd like to do is get the Car name from Cars table where CarID joins Cars and Results. When I try to do this though:
SELECT res.*, rac.*, u.*, t.*, c.*
FROM Results res
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
INNER JOIN Cars c USING (res.CarID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
I get the following error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.CarID) WHERE res.SeasonNumber = '1' AND res.LeagueID = '1' AND
Position = '1' ' at line 6
You can replace your USING clause with ON(),in USING() clause i guess you add the columns name that are same in other table you are joining but you placed the join in last and using alias res mysql won't allow this
INNER JOIN Cars c ON(res.CarID =c.CarID)
If you need to use USING() clause you need to adjust the join placements like
SELECT res.*, rac.*, u.*, t.*, c.*
FROM
Cars c
INNER JOIN Results res USING (CarID)
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
But ON() clause is more readable form
I have this database design:
**users_tbl**
id
username
name
**posts_tbl**
id
url
users_id *FK REFERENCE to users table*
**posts_contents_tbl**
id
posts_id *FK REFERENCE to posts table
title
description
date
views
click
isDeleted
I'm using this query
SELECT a.name,a.username,c.*
FROM users_tbl a
LEFT JOIN posts_tbl b ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC
Why I try to run this query it gives me NULL results, sample output is like this
But when I try to remove the ORDER BY c.id ASC it gives me this output:
That's not my expected result.
My expected result would be it will display the posts_contents_tbl in Ascending order at the same time it won't show some null values. Some users in my database doesn't have posts data in the posts_tbl so they should not show too.
How would I do that one? Your help would be greatly appreciated and rewarded!
Thanks!
PS: I already have thousands record in my database.
In that case, you have to use INNER JOIN instead of LEFT JOIN because you only want users with posts to show. The reason why there are Null values is because the records are based on table users_tbl and you've mentioned that some of them have no post. Right?
Try this:
SELECT a.name,
a.username,
c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
INNER JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY c.`date` DESC
I think this is what you are looking for:
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY IFNULL(c.id, 0) ASC;
If you really needs that posts_tbl data should not display if not available.And all data of posts_contents_tbl then you need a RIGHT JOIN and INNER JOIN .
The Query like :-
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b ON a.id = b.users_id
RIGHT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC;