$numQuery1 = "
SELECT d.*
, r.*
, AVG(c.on_time)+AVG(c.friendly)+AVG(c.language_skills)+AVG(c.professional) ranking
FROM comment c
LEFT
JOIN driver d
ON d.userid = c.driver_id
LEFT
JOIN driver_rental r
ON r.email = d.email
WHERE
(
$driver_rental IS NOT NULL
AND $driver_rental != ''
AND car_type_1 >= $_SESSION[car_type_1]
)
OR car_type_2>=$_SESSION[car_type_1]);
";
if the comment table have no driver_id record, the result is null, can I ignore comment table but in select statement cannot without it?
$numQuery1 = "SELECT driver.*, driver_rental.*, avg(comment.on_time)+avg(comment.friendly)+avg(comment.language_skills)+avg(comment.professional) as ranking
FROM comment
LEFT JOIN driver
ON
driver.userid = comment.driver_id
RIGHT JOIN driver_rental
ON
driver.email = driver_rental.email
WHERE
($driver_rental IS NOT NULL AND $driver_rental != '') AND (car_type_1>=$_SESSION[car_type_1] OR car_type_2>=$_SESSION[car_type_1])";
The idea is to not stress on driver table in either join. SO instead of second left join use right join with driver_rental
Related
I have following sql statement:
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
I using INNER JOIN on multi-database ,so that you can see sc and master_hankoya is difference databases
When I run it ,I get error :
Unknown column 'g.goods_id ' in 'on clause'
You can see g alias for table master_hankoya.goods,and goods_id is a column in this
I guess that I have problem with INNER JOIN
Please help me correct it
Update : I check again and take a silly problem ,have a special character in query make it failed to run
try this
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN
(select * from master_hankoya.goods ) g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
Don't use alias name for master_hankoya.goods like
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (master_hankoya.goods.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods
ON ha.WEBHINID = master_hankoya.goods.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
INNER JOIN sc.HINMTA ha
Change this line to:
INNER JOIN HINMTA AS ha
INNER JOIN master_hankoya.goods g
Change that line to
INNER JOIN goods AS g
The syntax for setting an alias for a table is:
table_name AS table_alias
This is my mysql query!
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
LEFT JOIN invoice
ON invoice.projects_id = projects.projects_id
LEFT JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND (SUM( invoice.invoice_amount) < projects.projects_cost
OR invoice.projects_id is null )
AND project_assign.project_completed_date IS NOT NULL
In this query i want select all row that:
Is not present in other table e.g. (in my case other table is
"invoice")
Or if persent then this condition must hold sum(invoice.invoice_amount) < projects.projects_cost
Thanks.
Divide your problem. Use a UNION. First use one query to select all records that are not present in the first table (Use a LEFT JOIN), union that with the result of your second query which would give you all records meeting your second condition (use an outer join)
//Select all records present in left table
//union
//select all records present in both tables matching your condition.
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
LEFT JOIN invoice
ON invoice.projects_id = projects.projects_id
LEFT JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND project_assign.project_completed_date IS NOT NULL
UNION
SELECT projects.projects_id,
projects.projects_title,
projects.projects_cost
FROM projects
INNER JOIN invoice
ON invoice.projects_id = projects.projects_id
INNER JOIN project_assign
ON project_assign.projects_id=projects.projects_id
WHERE project_assign.assigned_user_id=3
AND (SUM( invoice.invoice_amount) < projects.projects_cost
AND project_assign.project_completed_date IS NOT NULL
select projects.projects_id, projects.projects_title,projects.projects_cost
from projects
left join invoice
on invoice.projects_id = projects.projects_id
left join project_assign
on project_assign.projects_id=projects.projects_id
where project_assign.assigned_user_id=3 and
((select sum(invoice.invoice_amount) from invoice) < projects.projects_cost or invoice.projects_id is null )
and project_assign.project_completed_date is not null
You cannot put aggregation functions in the where clause. In this case, you can do the aggregation using a subquery and then do the comparison:
SELECT p.projects_id, p.projects_title, p.projects_cost
FROM projects p LEFT JOIN
(select i.projects_id, sum(invoice_amount) as invoice_amount
from invoice i
) i
ON i.projects_id = p.projects_id LEFT JOIN
project_assign pa
ON pa.projects_id = p.projects_id
WHERE pa.assigned_user_id = 3 AND
(i.invoice_amount < p.projects_cost OR i.projects_id is null ) AND
pa.project_completed_date IS NOT NULL;
I a developing in zend and have a rather large mysql query. The query works fine and i get the list I expect. I am doing this using Select->Where.... below is the query.
SELECT DISTINCT `d`.* FROM `deliverable` AS `d` INNER JOIN `groups` AS `g1` ON d.id = g1.deliverable_id INNER JOIN `groupmembers` AS `gm1` ON g1.id = gm1.group_id LEFT JOIN `connection` AS `c` ON d.id = c.downstreamnode_id LEFT JOIN `deliverable` AS `d1` ON c.upstreamnode_id = d1.id INNER JOIN `deliverable` AS `d2` ON CASE WHEN d1.id IS NULL THEN d.id ELSE d1.id END = d2.id INNER JOIN `groups` AS `g` ON d2.id = g.deliverable_id INNER JOIN `groupmembers` AS `gm` ON g.id = gm.group_id WHERE (g1.group_type = 100) AND (gm1.member_id = 1) AND (c.downstreamnode_id IS NULL OR d.restrict_access = 1) AND (g.group_type = 100 OR g.group_type = 110) AND (gm.member_id = 1) AND (d.deliverable_type = 110 OR d.deliverable_type = 100) GROUP BY CASE WHEN c.downstreamnode_id IS NULL THEN d.id ELSE c.downstreamnode_id END
Only problem is when I try to count the rows in a mysql query I only get 1 returned. below is the query
SELECT DISTINCT count(*) AS `rowCount` FROM `deliverable` AS `d` INNER JOIN `groups` AS `g1` ON d.id = g1.deliverable_id INNER JOIN `groupmembers` AS `gm1` ON g1.id = gm1.group_id LEFT JOIN `connection` AS `c` ON d.id = c.downstreamnode_id LEFT JOIN `deliverable` AS `d1` ON c.upstreamnode_id = d1.id INNER JOIN `deliverable` AS `d2` ON CASE WHEN d1.id IS NULL THEN d.id ELSE d1.id END = d2.id INNER JOIN `groups` AS `g` ON d2.id = g.deliverable_id INNER JOIN `groupmembers` AS `gm` ON g.id = gm.group_id WHERE (g1.group_type = 100) AND (gm1.member_id = 1) AND (c.downstreamnode_id IS NULL OR d.restrict_access = 1) AND (g.group_type = 100 OR g.group_type = 110) AND (gm.member_id = 1) AND (d.deliverable_type = 110 OR d.deliverable_type = 100) GROUP BY CASE WHEN c.downstreamnode_id IS NULL THEN d.id ELSE c.downstreamnode_id END
i generate this from by using the same 'select' that generated the first query but I reset the columns and add count in.
$this->getAdapter()->setFetchMode(Zend_Db::FETCH_ASSOC);
$select
->reset( Zend_Db_Select::COLUMNS)
->columns(array('count('.$column.') as rowCount'));
$rowCount = $this->getAdapter()->fetchOne($select);
This method works fine for all my other queries only this one i am having trouble with. I suspect it has something to do the 'CASE' I have in there but it is strange because I am getting the correct rows the the first query. Any ideas. Thanks.
FYI below are two queries that I have working successfully.
SELECT DISTINCT `po`.* FROM `post` AS `po` INNER JOIN `postinfo` AS `p` ON po.postinfo_id = p.id WHERE (p.creator_id = 1) ORDER BY `p`.`date_created` DESC
SELECT DISTINCT count(*) AS `rowCount` FROM `post` AS `po` INNER JOIN `postinfo` AS `p` ON po.postinfo_id = p.id WHERE (p.creator_id = 1) ORDER BY `p`.`date_created` DESC
In this one I have 4 rows returned in the first query and 'int 4' returned for the second one. Does anyone know why it doesnt work for the big query?
Move your DISTINCT.
SELECT COUNT(DISTINCT `po`.*) AS `rowCount` ...
Ok figured it out It was the GROUP BY that was causing only 1 result to be returned. Thanks Interrobang for you help I am sure that using DISTINCT incorrectly will have caused me a headache in the future.
Try using SQL_CALC_FOUND_ROWS in your query?
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
Using SQL_CALC_FOUND_ROWS is mysql-specific, but it's pretty nice for getting a full record count even when your initial query contains a limit. Once you get the count, don't include SQL_CALC_FOUND_ROWS in subsequent queries for extra records since that will cause extra load on your query.
Your initial query would be:
SELECT SQL_CALC_FOUND_ROWS DISTINCT `d`.* FROM `deliverable` AS `d` INNER JOIN `groups` ...
You'll have to do a subsequent call after your initial query executes to get the count by doing a SELECT FOUND_ROWS().
If you do a little searching, you'll find someone who extended Zend_Db_Select to include this ability.
i need an sql stament that will give me something like this where if a field is null it doesn't do the join
SELECT AdminID,tblapartments.NameNo, tblgarages.GarageID, tblclients.Name FROM tbladmin,tblclients,tblgarages,tblapartments WHERE tblclients.ClientID =tbladmin.ClientID AND
IF (tbladmin.ApartmentID != null)
{
tblapartments.ApartmentID = tbladmin.ApartmentID
}
AND If(tbladmin.GarageID != Null)
{
tblgarges.GarageID = tbladmin.GarageID
}
Unless I'm missing something, this should just be an outer join.
SELECT
AdminID,
tblapartments.NameNo,
tblgarages.GarageID,
tblclients.Name
FROM
tbladmin INNER JOIN tblclients ON tbladmin.ClientID = tblclients.ClientID
LEFT OUTER JOIN tblgarages ON tbladmin.GarageID = tblgarages.GarageID
LEFT OUTER JOIN tblapartments ON tbladmin.ApartmentId = tblapartments.ApartmentID
You can use LEFT JOINs, when the joined column does not exist in the other table the result is a lot of NULL fields:
SELECT AdminID,tblapartments.NameNo, tblgarages.GarageID, tblclients.Name
FROM tbladmin
INNER JOIN tblclients
ON tbladmin.ClientID = tblclients.CliendID
LEFT JOIN tblgarages
ON tbladmin.GarageID = tblgarages.GarageID
LEFT JOIN tblapartments
ON tbladmin.ApartmentID = tblapartments.ApartmentID
I do not believe that this type of if logic is SQL standard. You could possibly implement it in a procedural SQL langauge like PL/SQL, plpgsql ... however to accomplish what you after i think a left join what you should look at.
SELECT AdminID,tblapartments.NameNo, tblgarages.GarageID, tblclients.Name
FROM tbladmin a
join tblclients b on b.ClientID = a.ClientID
left join tblapartments c on c.ApartmentID = a.ApartmentID
left join tblgarges d on d.GarageID = a.GarageID
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.