I have following table in my database. I am trying to retrieve data from all three tables.
Table Structure:
User_New
User_ID
Name
Password
Email
User_Group_New
User_ID
Group_ID
Group_New
Group_ID
Group_Name
Issue: I am getting Duplicate records.
Query:
SELECT *
FROM `User_New`
INNER JOIN User_Group_New ON User_Group_New.User_ID = User_New.User_ID
INNER JOIN Group_New ON Group_New.Group_ID = User_Group_New.Group_ID
LIMIT 0, 30
SELECT DISTINCT *
FROM `User_New`
INNER JOIN User_Group_New ON User_Group_New.User_ID = User_New.User_ID
INNER JOIN Group_New ON Group_New.Group_ID = User_Group_New.Group_ID
LIMIT 0, 30
May that solve yours.
Related
I want to improve my search performance. I want to search the user against the services and specialty. user in listed as per the filter applied either by specialty or by services or can be both.There are 5 tables i want to get the data from all the table
I have below table structure
1) users :- id ,first_name,last_name
2) users_services :- id ,user_id,service_id
3) users_speciality :- id,user_id,specility_id
4) mst_services :- id,name
5) mst_speciality :- id,name
I have used this query to get the result and it works fine.
select u.id,first_name,last_name,location,services.name as service_name,speciality.name as specility_name from users as u
inner join users_services on u.id =users_services.user_id
Inner join mst_services as services on services.id=users_services.service_id
inner join users_speciality on u.id =users_speciality.user_id
Inner join mst_speciality as speciality on speciality.id=users_speciality.service_id WHERE speciality.name ="specificity one"
as per the normalization it seems correct.But when data is more than 1,00,000 that time joining too many table causes may create problem.
what should i do for filter the user according to services and specialty ?
Have you tried like this ?
SELECT users.id,first_name,last_name, mst_services.name as service, mst_speciality.name AS speciality
FROM users
LEFT JOIN users_services ON users.id = users_services.user_id
LEFT JOIN users_speciality ON users.id = users_speciality.user_id
LEFT JOIN mst_services ON service_id = mst_services.id
LEFT JOIN mst_speciality ON speciality_id = mst_speciality.id
WHERE users.id IN
(SELECT user_id FROM users_services
WHERE service_id = (SELECT id FROM mst_services WHERE name = "service one")
UNION ALL
SELECT user_id FROM users_speciality
WHERE speciality_id = (SELECT id FROM mst_speciality WHERE name = "spercial one"))
ORDER BY first_name,last_name,service,speciality
I'm trying to run a SQL query that joins with 2 other tables.
But, I need to still be able to use a WHERE condition in this query that uses the id of the main table. In this case the main table is timesheets.
I've tried the following but no rows were returned at all:
SELECT * FROM `timesheets` t
INNER JOIN `users` u ON `t`.`teacher_id` = `u`.`id`
INNER JOIN `schools` s ON `t`.`school_id` = `s`.`id`
WHERE t.id = 46
In this example I'm trying to filter to the row with ID 46 which definitely does exist.
If I do a simple query of just the timesheets table with the same where condition of id = 46 it works and returns that specific row.
When using EXPLAIN I get this message in the 'extra' column:
Impossible WHERE noticed after reading const table
Any ideas what I'm doing wrong?
As you have used INNER JOIN , so it will return data only if tables users and schools primary key has been used in timesheet as a foreign key.
use left join
SELECT * FROM timesheets t
LEFT JOIN users u ON t.teacher_id = u.id
LEFT JOIN schools s ON t.school_id = s.id
WHERE t.id = 46
I am new to SQL Join topic. I don't know why my SQL Query is not working properly. Here is the query:
SELECT * from post
INNER JOIN user ON post.id = user.id
INNER JOIN follower ON user.id= follower.id
WHERE follower.fid = 20 OR user.id < 1000
ORDER BY pid DESC LIMIT 7
If I use this query it works:
SELECT * from post
INNER JOIN user ON post.id = user.id
WHERE user.id < 1000
ORDER BY pid DESC LIMIT 7
But adding another INNER JOIN gave no output.
Update :
I am using 2 INNER JOINS so that I can show data from 3 table if there are no followers in follower table then show data from 2 tables(post and user) only where id in user table is less then 1000.
Its because you probably have no follower on the post:
SELECT * from post
INNER JOIN user ON post.id = user.id
LEFT OUTER JOIN follower ON user.id= follower.id -- changed JOIN type
WHERE follower.fid = 20 OR user.id < 1000
ORDER BY pid DESC LIMIT 7
Remember INNER JOIN only returns if it finds the match in all JOIN conditions. Any condition will fail and you won't get the row.
For example:
You have a post, but the user has no follower. INNER JOIN will skip the row. Here comes the OUTER JOIN! it will give you the user row even when no follower is found.
Consider 2 tables: USERS and TASKS for a task delivery app
USERS
userID | email | name
TASKS
userID | designator_userUNIQUE | destination_userUNIQUE
To get the destination_user full name from the users table I am using the following query that works fine:
SELECT *
FROM tasks
INNER JOIN users ON users.userID = tasks.destination_userUNIQUE
WHERE tasks.client='$clientUNIQUE'
ORDER BY tasks.date DESC, tasks.time DESC
However, if I want to get the full name from the designator_userUNIQUE using the below query, it simply generates an sql error (mysql_numrows() expects parameter 1 to be resource).
SELECT *
FROM tasks
INNER JOIN users ON users.userID = tasks.destination_userUNIQUE
INNER JOIN users ON users.name = tasks.designator_user UNIQUE
WHERE tasks.client='$clientUNIQUE'
ORDER BY tasks.date DESC, tasks.time DESC
Any ideas?
You need to give different aliases to each instance of the users table that you join with, to prevent ambiguity.
SELECT *
FROM tasks AS t
INNER JOIN users AS dest ON dest.userID = t.destination_userUNIQUE
INNER JOIN users AS desig ON desig.userID = t.designator_userUNIQUE
WHERE t.client='$clientUNIQUE'
ORDER BY t.date DESC, t.time DESC
I need to join tables
messages_between
users
status
I have this SQL:
SELECT messages_between.*, users.*, status.* FROM messages_between
LEFT JOIN users
ON users.uid = messages_between.user_2
LEFT JOIN status
ON status.uid = messages_between.user_2
WHERE messages_between.user_1 = 123
So this query works fine but the problem is when I don't have a row in messages_between.
The uid is code which is used in columns user_1 or user_2
Any suggestions?
Thanks
[EDIT]
My structures of tables:
users has columns: id, name, password, password_s, uid
status has columns: id, uid, status[if he/she is online or offline]
messages_between has columns: id, user_1, user_2, mid
So I want to show users.name, status.status, messages_between.user_2, messages_between.mid
Is this what you want?
SELECT messages_between.*, users.*, status.*
FROM users LEFT JOIN
messages_between
ON users.uid = messages_between.user_2 and messages_between.user_1 = 123 LEFT JOIN
status
ON status.uid = users.uid;
This keeps all users, providing additional information when there is an appropriate "message between".
You can try the following query. When you need null from the joined tables then you should not add the conditiona in where clause. Either you also check with null or add the conditiona in join clause.
Also you need to change the order of left join, becuase you want to retrive all the recrods from the users table and null values for the missing messages_between records and status records.
SELECT messages_between.*, users.*, status.* FROM messages_between
LEFT JOIN users
ON users.uid = messages_between.user_2 AND messages_between.user_1 = 123
LEFT JOIN status
ON status.uid = messages_between.user_2