Can anyone find what is wrong with this MS Access Query? When I try to execute it i receive an error about a missing Operator before the 2nd Left Join
SELECT * FROM (
SELECT GetitUsageTemp.MemberID,
GetitUsageTemp.IDNumber,
GetitUsageTemp.Title,
GetitUsageTemp.Initials,
GetitUsageTemp.Forenames,
GetitUsageTemp.Surnames,
GetitUsageTemp.CellNumber,
GetitUsageTemp.EmailAddress,
Nz(August.[AugustUsage],0) AS AugustUsage
FROM GetitUsageTemp
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS JulyUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #07/01/2013# And #08/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) Requests
ON GetitUsageTemp.MemberID = Requests.fk_Members_ID
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS AugustUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #08/01/2013# And #09/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) August
ON GetitUsageTemp.MemberID = August.fk_Members_ID
)GETIT
In Access you can only join two tables. If you need to join more tables, you need to group the first join together using parentheses, as if it was a new derived table. Then you can join another table to that group:
select
*
from
( ( Table1
LEFT JOIN Table2 ...
)
LEFT JOIN Table3 ...
)
LEFT JOIN Table4 ...
(I'm using awkward indentation to try to make the groups more clear)
Related
I created 2 simple table. tableA and table B, both only have 1 column 'po'.
I am trying to combine the 2 table together using FULL OUTER JOIN.
The error output is Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result.
So I guess pretty much my query is wrong. Appreciate any help here!
<?php
include 'db.php';
$sqlcount="SELECT DISTINCT po FROM testonly FULL OUTER JOIN testonly2 ON testonly.po=testonly2.po";
$resultcount = mysqli_query($conn,$sqlcount);
while($row = mysqli_fetch_array($resultcount)){
echo $row['po'];
}
?>
There is no FULL OUTER JOIN built in with MySQL. But, you could simulate your query via:
SELECT * FROM testonly t1
LEFT JOIN testonly2 t2 ON t1.po = t2.po
UNION -- use UNION ALL to retain common rows
SELECT * FROM testonly t1
RIGHT JOIN testonly2 t2 ON t1.po = t2.po
PHP code:
$sqlcount = "SELECT * FROM testonly t1 LEFT JOIN testonly2 t2 ON t1.po = t2.po UNION ALL SELECT * FROM testonly t1 RIGHT JOIN testonly2 t2 ON t1.po = t2.po";
Read here for more information: Full Outer Join in MySQL
if you need an united table you need and union You can't use FULL OUTER JOIN because this is not available in mysql ..but yuo can emulate with and an union select
The union clause implies that only distint value are merged for the two table ..
$sqlcount="SELECT po
FROM testonly
UNION
testonly2 ";
the union all implies the select of all the rows (also duplicated) from both the tables
$sqlcount="SELECT po
FROM testonly
UNION ALL
testonly2 ";
otherwise if you need a real join (bothe the column on the same rows ) you can use JOIN (inner join .. or left join, or right ) depending the way you need join the table
this is for inner join
$sqlcount="SELECT DISTINCT t1.po, t2.po
FROM testonly t1 INNER JOIN testonly2 t2 ON testonly.po=testonly2.po";
I'm trying to get data from multiple tales using LEFT OUTER JOIN but I'm getting a fatal error.
Table names, field names, db connection are correct.
$sql = "SELECT shipping_info.shipping_id, service1.service, package1.package_type, countries1.country AS fromCountry, countries2.country AS toCountry, countries3.country AS resiCountry, customer1.name,
FROM shipping_info
LEFT OUTER JOIN service_types AS service1 ON shipping_info.service_type = service_types.serviceType_id
LEFT OUTER JOIN package_types AS package1 ON shipping_info.package_type = package_types.packageType_id
LEFT OUTER JOIN customer_info AS customer1 ON shipping_info.customer_id = customer_info.customer_id
LEFT OUTER JOIN countries AS countries1 ON shipping_info.from_loc = countries1.country_id
LEFT OUTER JOIN countries AS countries2 ON shipping_info.to_loc= countries2.country_id
LEFT OUTER JOIN countries AS countries3 ON shipping_info.to_id = countries3.country_id
ORDER BY shipping_info.order_date DESC";
Fatal error: Call to a member function fetchAll() on a non-object in....
try changing your query to this:
SELECT s1.shipping_id,
s1.service,
p1.package_type,
c1.country fromCountry,
c2.country toCountry,
c3.country resiCountry,
c1.name
FROM shipping_info si
LEFT JOIN service_types s1 ON si.service_type = s1.serviceType_id
LEFT JOIN package_types p1 ON si.package_type = p1.packageType_id
LEFT JOIN customer_info c1 ON si.customer_id = c1.customer_id
LEFT JOIN countries c1 ON si.from_loc = c1.country_id
LEFT JOIN countries c2 ON si.to_loc= c2.country_id
LEFT JOIN countries c3 ON si.to_id = c3.country_id
ORDER BY si.order_date DESC;
you had multiple typos in the query itself with incorrect syntax.. also LEFT OUTER JOIN and LEFT JOIN are exactly the same
Also can you post how you are executing this query? you may have an issue with the actual method for executing it.
I'm not a MySQL expert, but this looks wrong. Consider your first join:
LEFT OUTER JOIN service_types AS service1
ON shipping_info.service_type = service_types.serviceType_id
You give table service_types an alias (a correlation name) of service1, but then don't use it in the ON part of the join. The first thing I would try is either get rid of the correlation name:
LEFT OUTER JOIN service_types
ON shipping_info.service_type = service_types.serviceType_id
...or use it:
LEFT OUTER JOIN service_types AS service1
ON shipping_info.service_type = service1.serviceType_id
Since you're using it in the names of the columns you're actually selecting, I'd go with using it in ON part of the join. Whichever, repeat with package_types and customer_info and then try.
Right off the bat I can see that you have an extra comma just before the FROM clause. This would cause an error and could result in the error you are getting as you would be running fetchAll on a non-object, i.e. the query that wasn't formatted properly.
Your table aliases are pretty long, so you could either shorten them or ditch them altogether and just use the full table name.
SELECT
si.shipping_id,
st.service,
pt.package_type,
c1.country AS fromCountry,
c2.country AS toCountry,
c3.country AS resiCountry,
ci.name
FROM shipping_info si
LEFT JOIN service_types st
ON si.service_type = st.serviceType_id
LEFT JOIN package_types pt
ON si.package_type = pt.packageType_id
LEFT JOIN customer_info ci
ON si.customer_id = ci.customer_id
LEFT JOIN countries c1
ON si.from_loc = c1.country_id
LEFT JOIN countries c2
ON si.to_loc= c2.country_id
LEFT JOIN countries c3
ON si.to_id = c3.country_id
ORDER BY si.order_date DESC;
Also, I've recently moved over to using MySQL Workbench. It's definitely worth checking out. I like it better than PHPMyAdmin. It's a better workflow for me and has cool tools like the reverse engineer tool that will build an ERD for you based on your tables. It's great for testing out your queries before using them in your PHP code.
MySQL Workbench
http://www.mysql.com/products/workbench/
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
we are developing portal like social network site with different concept...but for doing suggested connections we got strucked in mysql queries...
we are trying to take users with similat data ...
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
WHERE q.type_of_institution in
(
SELECT type_of_institution
FROM educonnect_user_qualification qi
WHERE qi.user_id = 3
)
AND q.college in
(
select college
from educonnect_user_qualification qc
where qc.user_id = 3
)
AND q.country in
(
select country
from educonnect_user_qualification qco
where qco.user_id = 3
)
AND a1.country in
(
select country
from educonnect_user_contact cc
where cc.user_id = 3
)
AND a1.state in
(
select state
from educonnect_user_contact cs
where cs.user_id = 3
)
Like this I am joining 10 tables ..but the problem is wherever i gave AND operator no result generated and if i gave OR operator it returns all users ..its the logic these operator will give output which I know..but for this problem i need different suggestions which would work effectively..or else the query can be changed with any other specific operator???
You didn't need all these WHERE IN conditions. You just need where educonnect_user.user_id = 3 since the educonnect_user table is joined with the other two tables educonnect_user_qualification and educonnect_user_contact, therefore the join will insure that for the user with id=3 the fields: type_of_institution, college, country,..( and other fields ) are exists in the two other tables for the same user, But you need to care about what type of join you need depending on what fields you want to select from your tables whether Left, right. So I think the following query what you are looking for:
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
where u.user_id = 3
Hope this will help ::
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
left join educonnect_user_qualification qi on (q.type_of_institution=qi.type_of_institution and qi.user_id=3)
left join educonnect_user_qualification qc on (q.college=qc.college and qc.user_id=3)
left join educonnect_user_qualification qco on (q.country=qco.country and qco.user_id=3)
left join educonnect_user_contact cc on (a1.country=cc.country and cc.user_id=3)
left join educonnect_user_contact cs on (a1.state=cs.state and cs.user_id=3)
I am trying to unify a pair of queries with a LEFT JOIN, so that I can perform an ORDER BY on a desired column.
Table A has an exclusive one-to-many relationship with Table B, and table B has an exclusive one-to-many relationship with Table C.
I want to get the maximum value of a column in Table C, for each row in Table A.
what I used to have was:
$tableA = SELECT * FROM tableA;
for each row in $tableA {
$maxValue = SELECT MAX(value) FROM tableC WHERE tableB_id IN (SELECT tableB_id FROM tableB WHERE tableA_id={$row['tableA_id']}) GROUP BY tableB_id;
}
and now I'm thinking along the lines of:
SELECT * FROM tableA LEFT JOIN (SELECT tableA_id, MAX(max_c_value) FROM (SELECT tableB_id, MAX(value) max_c_value FROM tableC GROUP BY tableB_id) t GROUP BY tableA_id) USING(tableA_id)
but I know that's gibberish. I am not sure where to go from here.
I'm finding it hard to explain the problem, sorry.
SELECT A.ID, MAX(C.MyField) as CField
FROM A
LEFT OUTER JOIN B
ON A.ID = B.A_ID
LEFT OUTER JOIN C
ON B.ID = C.B_ID
GROUP BY A.ID
You will get null value for CField if there is no rows corresponding.
select max(table3.field),table1.field from table1
join table2 on table1.id=table2.table1_id
join table3 on table2.id = table3.table2_id
group by table1.field
You were closer on the first try I think. You want something like this:
SELECT MAX(tableC.cValue) FROM tableA
LEFT JOIN tableB
ON tableA.tableA_id = tableB.tableA_id
LEFT JOIN tableC
ON tableB.tableB_id = tableC.tableB_id
http://www.w3schools.com/sql/sql_join.asp
I have managed to solve it on my own, only to come back here and find I was making things far too complicated for myself, getting lost in a sea of tables!
This code did actually work, but I have already replaced it with the simpler code suggested above:
SELECT * FROM A LEFT JOIN (
SELECT A_ID, MAX(val) FROM (
SELECT * FROM B LEFT JOIN (
SELECT B_ID, MAX(val) val FROM C GROUP BY B_ID
) x USING(B_ID)
) y GROUP BY A_ID
) z USING(A_ID);