how to mysql join works in my result - php

i have two tables one is tag_names which is connected to TABLE groups by foreign key g_id.i fire 3 different select query with mysql JOIN and output is in fig(a)
here table tag_names is:
tell me please how mysql JOIN works in my result

Here's one good article about MySQL Joins: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
And keep in mind, when joining two tables on g_id, MySQL will result all the possible rows.
This result will give you all possible joins from both tables:
SELECT * FROM `tag_names`
LEFT JOIN `groups` on `groups`.`g_id` = `tag_names`.`g_id` LIMIT 5
While this one will group both tables by g_id
SELECT * FROM `tag_names`
LEFT JOIN `groups` ON `groups`.`g_id` = `tag_names`.`g_id` GROUP BY `g_id` LIMIT 5

Related

SQL Query Phpmyadmin is not giving correct results having more than 3 tables with WHERE Clause

I am having relational database and trying to execute following query up to 3 tables It is giving me correct results i.e. 248 records of students in institute id 910, but when I try to make a query having more than 3 tables it gives me 19000+ results.
SELECT *
FROM Student,StudentRegistration,RefStudentType,RefGender,SubjectCategory
WHERE Student.student_id=StudentRegistration.student_student_id
AND StudentRegistration.reg_student_type_std_type_id = RefStudentType.std_type_id
AND Student.student_gender_gender_id = RefGender.gender_id
AND StudentRegistration.reg_student_subjectCat_sub_cat_id=SubjectCategory.sub_cat_id
AND Student.student_institute_inst_id=910;
`
Tried with JOIN as well but same 19000+ records incorrect results
SELECT * FROM Student INNER JOIN StudentRegistration ON student_id=student_student_id INNER JOIN RefReligion ON RefReligion.religion_id=Student.student_religion_religion_id INNER JOIN RefStudentType ON RefStudentType.std_type_id=StudentRegistration.reg_student_type_std_type_id WHERE student_institute_inst_id=910;
Any solution, query logical errors or something new
I think this is due to having several records for one data. For example, there might be several records in the SubjectCategory table for id = '910'
It is best to use left/right/inner/outer joins without using from tbl1, tbl2
What I suggest is to check the tables one by one with the id.

How to join two tables by 5 columns? Sql

I have two tables called customers and wantslist.
I want to join the columns customername, customeraddress , creditlimit , bytitle and byauthor.
How can I write a query that can show those column together in a table?
The first table is customers. It has the columns customeraddress , customername and creditlimit.
The second table is wantslist. It has the columns bytitle and byauthor.
How can I write a query that can join those 5 columns into 1 table?
Without knowing your specific strucutre you would use a SQL query similar to...
SELECT c.customername, c.customeraddress, c.creditlimit, w.bytitle, w. byauthor
FROM customers as c
JOIN wantslist as w on c.customerid = w.customerid
SELECT customers.customername, customers.customeraddress, customer.creditlimit, wantslist.bytitle, wantslist.byauthor
FROM wantslist
INNER JOIN customers
ON *{your matching condition}*;

Query records from a table with criteria based on another table

Is it possible to query records from a table with criteria based on another table? I'm not very good at MySQL JOIN so can some one point me to the right direction? here's the case:
Table 1: tbl_users(id,username,points)
Table 2: tbl_items(id,user_id,title,cost)
I want to query all items for all user_ids but only if their cost is less than the number of points that this user_id has. I know I can go on it one by one in a loop but I'm looking for the smarter way.
Thank You,
Join the two tables based on the user id and in where add your condition need to be checked.
SELECT * FROM `tbl_users` AS `user`
LEFT JOIN `tbl_items` AS `item` ON (`user`.`id` = `item`.`user_id`)
WHERE `item`.`cost` < `user`.`points`
select a.* from tbl_users as a join tbl_items as b on a.id=b.id where a.points>b.cost;
SELECT `*` FROM `tbl_users` JOIN `tbl_items` ON (`tbl_users`.`id`= `tbl_items`.`user_id` and `tbl_items`.`cost`<`tbl_users`.`points`)

SQL query for Selecting from Multiple Tables in single database

I am having 3 tables (c19 , c19b2, g26) in a database
I want to write a SQL Query to search and display all fields of the matched record.
I am using following query:
$query = "SELECT * FROM c19,c19b2,g26 WHERE armyno LIKE '%$searchTerm%'";
But it only works for table c19,
Data from the other 2 tables is not fetched.Each table has a field armyno
Please help me with this
Thank you.
Alright, you are not looking for a JOIN, but a UNION.
SELECT * FROM c19 WHERE armyno LIKE '%$searchTerm%'
UNION
SELECT * FROM c19b2 WHERE armyno LIKE '%$searchTerm%'
UNION
SELECT * FROM g26 WHERE armyno LIKE '%$searchTerm%'
That will let you query all three tables at the same time.
Which DB are you using? This would have worked in SQL Server. However, notice you are doing a cross join of every record to every record... usually you only want to match some records by restriction of a matching key, for example:
select
*
from a
left join b on b.somekey = a.somekey
left join c on c.someotherkey = b.someotherkey
In SQL server you can just say *, but I'm taking it that in your DB engine that didn't work, so try specifying which table. This may in some environments require aliasing as well:
select
a.*,
b.*,
c.*
from tableA as a
left join tableB as b on b.somekey = a.somekey
left join tableC as c on c.someotherkey = b.someotherkey
Generally, you should see the columns from the first table, followed by the columns from the second table, followed by columns from the third table for a given row. If you wanted to get all columns from all tables, but separately, then that would be 3 separate selects.
Lastly, if all 3 tables have "armyno" then I'd expect it to throw an ambiguous field error. In such case you'd want to specify which table's "armyno" field to filter on.

MySQL Inner Join involving three tables

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

Categories