Select two values from two different tables - mysql

I have two tables, they look like this:
Table 1
==========================
id_tab1 | worker | number
==========================
1 | Adam | 123123
Table 2
==========================
id_tab2 | worker | number
==========================
1 | Adam | 123456
They both don't have anything in common. Sometimes worker might be in both of them and I want to count every row from both tables and show them like:
===============================================
worker | count_numbers_tab1 | count_numbers_tab2
===============================================
Adam | 1 | 1
I tried with INNER JOIN but it shows weird numbers.
EDIT 1:
As with Abhilekh answer I ended up with following query(real example):
SELECT druga_klasa.pracownik, COUNT(druga_klasa.numer_zlecenia), COUNT(zlewy_zlomy.numer_zlecenia) FROM druga_klasa
FULL JOIN zlewy_zlomy on druga_klasa.pracownik=zlewy_zlomy.pracownik
GROUP BY pracownik;
and thats how real tables looks like
and I've got an error saying
Unknown column druga_klasa.pracownik in field list.

What do you mean by they both don't have anything in common?
SELECT table1.worker, COUNT(table1.number), COUNT(table2.number) FROM table1
INNER JOIN table 2 on table1.worker=table2.worker
GROUP BY worker;
But this will only work if the column worker is common to both.

Related

MySQL JOIN to show required output

Is there a way I can select all columns from two tables in MySQL, but only show the data in the end column if a match is found in both tables?
For example…
Main table (contacts)
id| token | Name
------------------
1 | ABC | Test person 1
2 | DEF | Test person 2
3 | GHI | Test person 3
Subscriptions table (subscribed)
id| contact_token
-------------------
1 | ABC
Desired output
id| token | Name | contact_token
------------------------------------------
1 | ABC | Test person 1 | ABC
2 | DEF | Test person 2 |
3 | GHI | Test person 3 |
The contents of contact_token is show (ABC) because it appears in both tables.
Thanks
You should consider storing the subscription data in the main contacts table, unless you can have multiple subscriptions per user. If the latter is the case, then you have to consider if you want to see the subscription tokens within one line in the output or in multiple lines.
The basic query is simple, you need to left join the subscribed table to the contacts:
select contacts.id, token, name, contact_token
from contacts left join subscribed
on contacts.id=subscribed.id
Yes. This is what a LEFT JOIN is for:
You select all records from table contacts and you do a left join on the subscribed one.
SELECT subscribed.id,token,name,contact_token
FROM contacts LEFT JOIN subscribed
ON contacts.id = subscribed.id;
For the records that are only in the first table you will get NULL in the last column.

MYSQL query fetching DATA from two table using IN method one as composition of multiple data

I have two tables
one as td_job which has these structure
|---------|-----------|---------------|----------------|
| job_id | job_title | job_skill | job_desc |
|------------------------------------------------------|
| 1 | Job 1 | 1,2 | |
|------------------------------------------------------|
| 2 | Job 2 | 1,3 | |
|------------------------------------------------------|
The other Table is td_skill which is this one
|---------|-----------|--------------|
|skill_id |skill_title| skill_slug |
|---------------------|--------------|
| 1 | PHP | 1-PHP |
|---------------------|--------------|
| 2 | JQuery | 2-JQuery |
|---------------------|--------------|
now the job_skill in td_job is actualy the list of skill_id from td_skill
that means the job_id 1 has two skills associated with it, skill_id 1 and skill_id 2
Now I am writing a query which is this one
SELECT * FROM td_job,td_skill
WHERE td_skill.skill_id IN (SELECT td_job.job_skill FROM td_job)
AND td_skill.skill_slug LIKE '%$job_param%'
Now when the $job_param is PHP it returns one row, but if $job_param is JQuery it returns empty row.
I want to know where is the error.
The error is that you are storing a list of id's in a column rather than in an association/junction table. You should have another table, JobSkills with one row per job/skill combination.
The second and third problems are that you don't seem to understand how joins work nor how in with a subquery works. In any case, the query that you seem to want is more like:
SELECT *
FROM td_job j join
td_skill s
on find_in_set(s.skill_id, j.job_skill) > 0 and
s.skill_slug LIKE '%$job_param%';
Very bad database design. You should fix that if you can.

Sql serialize columns into one record for search

I would like to create a search feature which looks in multiple tables and columns and return the main idea. For example:
(This is a very simplified scenario, and the real tables have way more columns in which I would like to search)
table 'lead':
id | name | created
1 | john | 1/1/2014
2 | jack | 2/1/2014
table 'notes':
id | lead_id | created | note
1 | 1 | 1/1/2014 | lead added
2 | 1 | 1/2/2014 | some change occurred
3 | 2 | 2/1/2014 | lead added
4 | 2 | 2/2/2014 | some updates
I would like to provide for example the string "2014" which would return lead.id 1,2
Or look for "updates" which would return lead.id 2.
I can either run a join every time the search is performed, or somehow create a view which looks like this:
id | text
1 | john 1/1/2014 1/1/2014 lead added 1/2/2014 some change occurred
2 | jack 2/1/2014 2/1/2014 lead added 2/2/2014 some updates
This way, the search would be in one table and provide a fairly quick results, the "work" is updating the records per id every time tables 'notes' is updated.
I know a view is the best way to do this, but I am not clear on how to serialize a join result into one textual column. Of course it is fairly easy with php (or any back end script) but I was wondering if this can be done in a manner similar to when a view is constantly updated.
Thank you for your assistance.
Use GROUP BY and GROUP_CONCAT functions, somethink like this:
SELECT n.lead_id, CONCAT(l.name, " ", l.created, " ",GROUP_CONCAT(CONCAT(n.`created`," ",n.`note`), " ")) as text
FROM notes n
JOIN lead l ON l.id = n.lead_id
GROUP BY n.`lead_id`
ORDER BY n.`created` DESC
Inner CONCAT forms submessage 1/1/2014 lead added, GROUP CONCAT joins then and the outer CONCAT forms result message john 1/1/2014 1/1/2014 lead added 1/2/2014 some change occurred
Here is SQL Fiddle example

Best method of organizing database?

MORE DETAILS:
Both of you recomend using JOIN. But the main problem is how to assign multiple SUBJECTS PER EACH CLASS without using multiple duplicate values. I will have ~200 de classes, with ~30 subjects per class. That means if 2 classes share the same 20 subjects, i will have 40 rows, all with "class_id = 1" but with "subjects_Id =1, subjects_id=2, etc" Its not very ergonomic. Any other ideas? Thanks for your time!
So, I am here again asking for your time and help friends.
I have a database that its almost ok. But I am stuck at trying how to link multiple values from a table to on collumn on another.
Let me be more explicit.
I have this table:
CLASSES
id | class_name | Matters |
-----------------------------
1 | Class1 | 13.4.2013 |
2 | Class2 | 14.4.2013 |
And this table:
Subjects
mat_id | show title |
-----------------
1 | English |
2 | French |
Now the problem is this. Each CLASS (e.g. CLASS1) should be able to study more Subjects at once. For example, CLASS 1 should be linked with subject (mat_id) 1, 3, 5, 6.
How to do this without repeating myself, and optimize the database? I tought that I should do it like so, but its not convenient :
CREATE A NEW TABLE named
SUBJECTS_PER_CLASS
id | class_id | mat_id |
----------------------------
1 | 1 | 1 |
2 | 1 | 3 |
BUT then I dont know how to query it. Any ideas? Any help will be greatly appreciated!
THANKS!
SELECT
*
FROM
CLASSES
JOIN
SUBJECTS_PER_CLASS
ON
CLASSES.ID = SUBJECTS_PER_CLASS.class_id
JOIN
Subjects
ON
Subjects.id = SUBJECTS_PER_CLASS.mat_id
You can use join command.
Reference 1
Reference 2

mutual non-mutual friend query mysql

Hello everyone I have been trying this for ages now.
I have read many questions here and tried adapting the varied solutions to my needs but without results.
History:
for an event there are many participants.
the participants all meet one another at the event and give out "likes" to all the other participants they actually like.
At the end of the event the admin inserts all the likes for each participant of THAT event, and the system will find the mutual likes (friendship)
Problem:
While inserting the likes i would like (pun) the system to detect weather a friendship is already established (from other events also) and if so avoid to display that user name when setting the likes.
Here are the tables that I'm using (mysql)
wp_fd_users
id | user_name | user_gender | .. etc
wp_fd_matches
id | event_id | event_user_id | event_user_match_id | ... etc
Example of the match table
1 | 1 | 1 | 3 | ...
2 | 1 | 1 | 4 | ...
3 | 1 | 2 | 6 | ...
4 | 1 | 3 | 1 | ...
where you can clearly see that 1 <-> 3 have a mutual relationship and 1 likes 4 but not mutually.
I would need a query that returns all results that AVOID relationships that have been established in one single event.
An occurance like this:
1 | 1 | 1 | 3 | ...
2 | 1 | 1 | 4 | ...
3 | 1 | 2 | 6 | ...
4 | 2 | 3 | 1 | ...
would not trigger the like because it happens in two separate events
Hope it's clear
YOur question is a little unclear. I am going by: "I would need a query that returns all results that AVOID relationships that have been established in one single event."
The following self join accomplishes this:
select m1.*
from wp_fd_matches m1 left outer join
wp_fd_matches m2
on m1.event_id = m2.event_id and
m1.event_user_id = m2.event_user_match_id
m1.event_user_match_id = m2.event_user_id
where m2.id is null
It looks for the matching record. However, by using a left outer join, it is getting all records. It then filters out the ones with a match.