i would get details from two MySQL tables
tables structure as shown
table:App
|AppID|AppName|AppType|
table:AppRelease
|AppReleaseID|AppID|ReleaseDate|ReleaseVersion|
and written query as shown below
$query="
SELECT
A.*,
B.ReleaseDate,
B.ReleaseVersion
FROM App AS A
INNER JOIN AppRelease AS B
WHERE A.AppID = B.AppID
";
i get the values when appid is in both tables
but i also want to get values from App table though i dont have data in AppRelease release table
is it possible to write query please help me
Your requirement shouldn't be inner join.
Use left join:
$query= "SELECT A.*,B.ReleaseDate,B.ReleaseVersion
from App as A LEFT JOIN AppRelease as B
ON A.AppID=B.AppID";
Related
I have written a query using inner join which consist of multiple tables
SELECT *
FROM admin_info
INNER JOIN admin_login
INNER JOIN gender
INNER JOIN admin_type
INNER JOIN area
INNER JOIN document_type
INNER JOIN permissions
ON admin_login.admin_id=admin_info.admin_id
AND admin_type.admin_type_id=admin_info.admin_type_id
AND area.area_id=admin_info.area_id
AND document_type.document_id=admin_info.document_id
AND permissions.permission_id=admin_info.permission_id
The above query works but gives multiple results of the same record or duplicate records
Please help me fix the query or provide with an alternative to this query
also please suggest me ways to optimize the query for faster processing??
The above Query
SELECT *
FROM admin_info
INNER JOIN admin_login
INNER JOIN gender
INNER JOIN admin_type
INNER JOIN area
INNER JOIN document_type
INNER JOIN permissions
ON admin_login.admin_id=admin_info.admin_id
AND admin_type.admin_type_id=admin_info.admin_type_id
AND area.area_id=admin_info.area_id
AND document_type.document_id=admin_info.document_id
AND permissions.permission_id=admin_info.permission_id
Below Given is the screen shot of the results.
I want only one record per person and not duplicate records?
Apart from that when i use foreach/while loop the results vary please help?
Actually The problem was with gender table which had no reference in the query as soon as i removed the gender table inner join it worked as expected!
I have been looking and cant find an answer to what im trying to do.
I dont know if a query can be created in the following way.
$sql_call = "SELECT table.item,table.item,table.item FROM cust
LEFT JOIN contact ON cust.id = contact.client_id
LEFT JOIN survey_audit ON cust.id = survey_audit.cust_id
WHERE cust.clinic='$clinic_id' AND contact.participate='1' AND survey_audit.survey_id != '$post_survey_id'";
The query above, does not do what Im trying to do, and that is:
Get data from tables WHERE cust.clinic=something AND contact.participate=something AND (this is the part im not sure about) inside Survey_audit table, there is no row with this id.
Is it possible to ask sql to find a result where something=something AND is no row in specific table?
You are sort of on the right track. You simply need to look for cases where survey_audit.survey_id is NULL.
SELECT table.item,table.item,table.item
FROM cust
LEFT JOIN contact
ON cust.id = contact.client_id
LEFT JOIN survey_audit
ON cust.id = survey_audit.cust_id
WHERE cust.clinic='$clinic_id'
AND contact.participate='1'
AND survey_audit.survey_id IS NULL
Here is a very useful resource for helping you determine how to form more complex join scenarios. Your case is the 4th example on this page.
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
You can exclude all the elements of the table using a subquery:
$sql_call = "SELECT table.item,table.item,table.item FROM cust
LEFT JOIN contact ON cust.id = contact.client_id
LEFT JOIN survey_audit ON cust.id = survey_audit.cust_id
WHERE cust.clinic='$clinic_id' AND contact.participate='1' AND survey_audit.survey_id NOT IN (SELECT survey_id FROM Survey_audit);
Yes it is possible, you should read more about other types of joins in mysql there are 4 types of joins
INNER JOIN (JOIN) - matching id's in both tables
LEFT JOIN - matching id in table A and can be null in Table B
RIGHT JOIN - matching id in table B and can be null Table A
OUTER JOIN - can be null in both tables
Recommend you to read the following article
http://www.sitepoint.com/understanding-sql-joins-mysql-database/
So for your question I guess you should use RIGHT JOIN survey_audit instead of LEFT JOIN survey_audit
Here is my 2 sql query on the first query i want to use it as a table and on the 2nd query i want to join the 1st query which i assume a table
e.g.
First query:
SELECT sale.salesManID, sale.companyName, sale.sellingPrice
from sale Inner Join carModel
On (sale.companyName = carModel.companyName) AND (carModel.size>10) ;
Second Query:
SELECT salesMan.salesmanID, salesMan.name, SUM(firstQuery.sellingPrice)
FROM salesMan
INNER JOIN firstQuery
ON salesMan.salesmanID = firstQuery.salesManID
GROUP BY salesmanID, name;
How can i do it ? Thanks in advance.
If I understand the question correctly (although I'm not sure why you'd want to do it this way...):
IF object_id('tempdb..#firstQuery) IS NOT NULL DROP TABLE #firstQuery;
SELECT sale.salesManID, sale.companyName, sale.sellingPrice
INTO #firstQuery
FROM sale Inner Join carModel On (sale.companyName =
carModel.companyName) AND (carModel.size>10) ;
SELECT salesMan.salesmanID, salesMan.name,
SUM(firstQuery.sellingPrice) FROM salesMan INNER JOIN #firstQuery ON
salesMan.salesmanID = firstQuery.salesManID GROUP BY salesmanID,
name;
it's not a clear question. do you mean using subquery on join???
maybe you can take a look to this threads:
mysql subquery inside a LEFT JOIN
MySQL correlated subquery in JOIN syntax
or just google mysql join subquery
also you can take a look this post: How can my application benefit from temporary tables?e
I am working with four tables:
query,
store,
cluster_group,
tv_region
I want to retrieve query records belonging to a particular TV Region record.
A query record either belongs to a record in the store or cluster_group tables. The query table has 'store_id' and 'cluster_group_id' columns. Either will be null whilst the other will refer to a record in the store or cluster_group table. The store and cluster_group tables both have a 'tv_region_id' column.
To retrieve query records that belong to a TV Region record that has an id = 2, I wrote the following SQL statement:
SELECT query.id AS query_id, cluster_group.name as cluster_name, cluster_group.tv_region_id as cluster_tv_region, store.store_name
FROM query
INNER JOIN cluster_group
ON cluster_group.id=query.cluster_group_id
INNER JOIN store
ON store.id=query.store_id
WHERE cluster_group.tv_region_id = 2
AND store.tv_region_id = 2;
The problem is that it returns zero records even though there are query records that belong to the specified TV Region (via a cluster group or store record). I may have misunderstood how 'inner join' works.
I'm working with Doctrine 2 and have attempted the query using the left join but it still returns nothing, I guess this is because its returning null values too. How do I get it to return only the query records I am interested in and no null values?
Appreciate if someone can point me in the right direction to retrieve the relevant query records.
You will need to use OUTER joins, or possibly LEFT
INNER joins will only return data where there is a matching record on both sides. As you state in your question you only get a match on one side or the other, therefore there is never going to be a matching record across both store and cluster
Jeff Atwood has a good explanation of the join types on his blog http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Try this:-
SELECT query.id AS query_id, cluster_group.name as cluster_name, cluster_group.tv_region_id as cluster_tv_region, store.store_name
FROM query
LEFT OUTER JOIN cluster_group
ON cluster_group.id=query.cluster_group_id
AND cluster_group.tv_region_id = 2
LEFT OUTER JOIN store
ON store.id=query.store_id
AND store.tv_region_id = 2
EDIT - no experience of doctrine, but you could try using a UNION:-
SELECT query.id AS query_id, cluster_group.name as cluster_name, cluster_group.tv_region_id as cluster_tv_region, NULL AS store_name
FROM query
INNER JOIN cluster_group
ON cluster_group.id=query.cluster_group_id
AND cluster_group.tv_region_id = 2
UNION
SELECT query.id AS query_id, NULL as cluster_name, NULL as cluster_tv_region, store.store_name
FROM query
INNER JOIN store
ON store.id=query.store_id
AND store.tv_region_id = 2
It works like this for me:
from table1 inner join table2 on table1.id = table2.id
inner join table3 on table1.id = table3.id
I have three different SQL tables I need to join:
table "internet" with columns id|type|status
table "type_list" with columns id|type_name
table "status_list" with columns id|status_name
I want to output text from the two other tables (type_list, status_list) but not values as numbers which currently I have in table "internet".
I also don't want to make lazy programming - PHP array to make ID's equal to something like
$type_list = array("1"=>"VDSL2","2"=>"ADSL");
$status_list = array("1"=>"Pending","2"=>"Active");
because the text is already in the tables, i just dont know how to join them and output the text as query combined together in one query.
Use JOIN
SELECT i.id, type_name, status_name
FROM internet i
LEFT OUTER JOIN type_list t ON t.id = i.type
LEFT OUTER JOIN status_list s ON s.id= i.status
Read the MySQL doc for more informations.
Just write the select with the fields you want.
select internet.id,type_name,status_name from internet
inner join type_list
on type_list.id=internet.id
inner join status_list
on status_list.id=internet.id
For this you need a LEFT JOIN, like so:
SELECT i.id, t.type_name, s.status_name
FROM internet AS i
LEFT JOIN type_list AS t ON t.id = i.id
LEFT JOIN status_list AS s ON s.id= i.id
From your question, it is unclear what field you would like to join the queries on. In the above example, the queries are joined on the id field.
Please also note that the AS is not actually necessary, I have just put it in there to make it clear what is going on