I have two database tables one called clients, the other one users. I'm trying to select single field "email" from both tables. Note, that email field is in both tables.
Table clients contains 10 records, while users has 5 records.
The below query works, but for some reason records are repetitive, so instead of getting 15 records in total, I end up with 50. Seems like records from one table are multiplied by records from other table.
SELECT
clients.email,
users.email
FROM
clients,
users;
Any help would be appreciated.
Your query returns cartesian product
You can simply use union all
select email from clients
union all
select email from users
You are getting 50 rows because cross join is happening there, go for inner join or left join or right join as per your choice, most preferably inner join...
Take some common attributes in both like id or something...
select clients.email, users.email from clients,users where clients.id=users.id...
try renaming the fields
For Clients table
clients_email
For Users table
users_email
then query as
SELECT clients.clients_email, users.users_email FROM clients, users WHERE clients.clients_email = users.users_email;
Hope it helps
Related
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.
I am trying to select everything from multiple tables where there are 2 conditions.
But they only bring back a single result from the second table, instead of everything
Here is the MySql code
SELECT *
FROM `core_users`.`users`
LEFT JOIN `core`.`orders`
ON `core_users`.`users`.`uid` = `core`.`orders`.`uid`
LEFT JOIN `core_users`.`crm`
ON `core_users`.`users`.`uid` = `core_users`.`crm`.`uid`
WHERE (`core_users`.`users`.`signup_timestamp` BETWEEN '1476626400' AND '1476712800'
OR (`core_users`.`crm`.`type` = 'refresh_5in5' AND `core_users`.`crm`.`value` BETWEEN '1476626400' AND '1476712800') )
This just brings back 1 result from the crm table. However I want it to bring back all the results from the crm table.
How do I bring back everything from users, orders, and crm while having a WHERE clause on both tables?
Try select users.*, orders.*, crm.*
Please note that, in such case if you have common columns in any of these table like id in every table it would cause an ambiguous column name error. To get rid of that you need to specify these with alias name users.id as user_id, orders.id as order_id, crm.id as crm_id and so on.
We need to grab the last and newest 20 entries from different tables. However, the GROUP BY statement skips records because we are working with LEFT JOIN on tables.
All these records are linked to unique persons in another table. We store these person's id's in an array for more queries later.
We have a few tables (in which all those person id's are stored) and we want to get them sorted and grouped.
The tables are like this:
SELECT lastRecord+personID FROM t1
SELECT lastRecord+personID FROM t2
SELECT lastRecord+personID FROM t3
SELECT lastRecord+personID FROM t4
WHERE t5.Essential_Column_Name = '1'
GROUP BY personID
ORDER BY 'all the latest entries'
LIMIT 20
With that, the relevance of all the latest entries should be equal.
We do have a timestamp column as well. Perhaps that might work better.
Any input is highly appreciated!
For people looking for an answer on this; this is the right post, answer and update to this Q:
UNION mysql gives weird numbered results
With thanks to all for the ideas and providing the paths to the right solution.
$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments
FROM totals
INNER JOIN users
GROUP BY totals.idseller;");
When i add the INNER JOIN the sum value is changed. Why?
In my SQL table i have one record in totals width this value: 8943.09 but when i do the some the result is giving me this value: 44715.45
What i am doing wrong?
$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments FROM totals
INNER JOIN users ON totals.idseller = users.idseller
GROUP BY users.UserName;");
Use this Hope this will help you.
When you INNER JOIN to another table, the returned data set is modified to only include rows that exist in both tables. In this case it is likely that there are rows in 'totals' that do not have a matching row in users - either the totals.idseller field might accept null values, or data has become orphaned when matching users have been deleted or edited.
If you want all data in 'totals' regardless of matching user you would user a LEFT JOIN instead in ms-sql, I suspect a similar approach will work in my-sql
You should give an "on" based on the ids. Such as like
inner join users on users.id = totals.idseller
Otherways the sql server will combine all possible rows in the tables, which is most cases not what you wish.
Because when you are adding inner join in your SQL Query, it means you are selecting the data which is common in both the tables.
EX:
SELECT * FROM TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.ID = TABLE_B.ID
If you are joining users table which contains 5 records. By joining table, as there is no any column mapping, this sum-up 5 times and this is reason for showing different values.
Please let me know something wrong in it.
Thanks,
Umehs
I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields