Join from two tables mysql - mysql

I have a doubt where trying to join two tables by a previous search. I've looked several solutions and read some chapters in a mysql book but I think I'm pretty close to the right answer but still not get it
I have this table "userprocess":
idProcess username state
------------------------------------------
1 blisssing 3
2 enriquecalvera 1
2 africabaluja 2
1 enriquecalvera 3
2 blisssing 1
The primery key for this table is the union of idProceso+username.
I have this other table "user":
index username pass active tipeUser .... so on
----------------------------------------------------------------- ----
1 blisssing 6OiZVVUPi3LDE 1 user
2 carmen 6OOtfrXB2Nu5. 1 user
3 consuelo 6OgdhVSkr1VDs 1 user
4 africabaluja 6OoPtGjWMQARE 1 user
5 enriquecalvera 6O6tvHg.122uQ 1 user
The thing is I want to show the join of the two tables but with a search within the first table. If I run this query
SELECT username FROM userprocess where idProcess='1' ORDER BY state
I get this:
username
---------
blisssing
enriquecalvera
which is what I am looking for, but I want to show all the fields in the "user" table for those usernames ordered by idProceso. So I run this other query:
SELECT *
FROM
user u,
userprocess p
WHERE
u.username=p.username
AND u.username IN (
SELECT username
FROM userprocess
where idProcess='1'
ORDER BY username
) ORDER BY p.state
I got this:
username pass active tipeUser idProcess state
----------------------------------------------------------------------
blisssing 6Od3nSkfOiwlg 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 1 3
blisssing 6Od3nSkfOiwlg 1 user 2 3
But this is not what I want I just want the same two results as in the previous query but with all the columns of the result of joining the two tables..
I know there is a lot of questions like this, but I have tried a lot of things and still not having the desire result..
What am I missing?
thank you, if you have any qestion or doubt just ask :)

The reason you're seeing multiple results is because you're joining on just the username, but of course the userprocess table has 2 rows where username = enriquecalvera. Your subquery is correctly only returning the 1 row you're interested in (where idprocess = 1) but as your join is seperate to this, and therefore doesn't include the idprocess = 1 condition, you're getting both rows back.
You should just do this in one step with a join like this:
SELECT *
FROM
user u
INNER JOIN userprocess p on u.username=p.username and p.idProcess='1'
ORDER BY p.state

Related

Histogram using to data tables (SQL query)

I want to make a histogram of the number of comments per user in January 2019 (including the once that haven't commented)
The tables I'm working with look like this:
id
Name
1
Jose
2
Pedro
3
Juan
4
Sofia
user_id
Comment
Date
1
Hello
2018-10-02 11:00:03
3
Didn't Like it
2018-06-02 11:00:03
1
Not so bad
2018-10-22 11:00:03
2
Trash
2018-7-21 11:00:03
I think I'm overcomplicating it. But here is my try:
#Here I'm counting how much comments are per person that have commented.
CREATE TABLE aux AS
SELECT user_id, COUNT(user_id)
FROM Undostres
GROUP BY user_id;
#With the following code, I end up with a table with the missing values (ids that haven't commented)
CREATE TABLE Test AS
SELECT DISTINCT user_id +1
FROM aux
WHERE user_id + 1 NOT IN (SELECT DISTINCT user_id FROM aux);
ALTER TABLE Test RENAME COLUMN user_id +1 TO ;
INSERT INTO Undostres (user_id)
SELECT user_id FROM Test;
It returns an error when I try to rename user_id+1 with other name. So I can't keep going.
Any suggestions would be great!
I would do it this way:
CREATE TABLE aux AS
SELECT Users.user_id, COUNT(Undostres.user_id) AS count
FROM Users
LEFT OUTER JOIN Undostres USING (user_id)
GROUP BY Users.user_id;
I am assuming you have a table Users that enumerates all your users, whether they have made any comments or not. The LEFT OUTER JOIN helps in this case, because if there are no comments for a given user, the user is still part of the result, and the COUNT is 0.

how to select specific rows in some condition

I tried to write a query that selects rows with steps that both user 1 and user 2 did, with combined number of times they did the step (i.e., if user 1 did step 1 3 times and user 2 did 1 time then the count should show 4 times.)
when I put condition as user_id=1, user_id=2 there is no error but it return nothing, when it should return some rows with values.
there is table step, and step taken
and table step has column id, title
table step_taken has column id, user_id(who performs steps), step_id
i want to find step that both of two user whose id 1,2 did
and also want to have the value as count added up how many times they performed that step.
for example if user id 1 did step named meditation 2 times,
and user id 2 did step named meditation 3 times,
the result i want to find should be like below ;
------------------------------
title | number_of_times
------------------------------
meditation| 5
------------------------------
here is my sql query
select title, count(step_taken.step_id)as number_of_times
from step join step_taken
on step.id = step_taken.step_id
where user_id = 1 and user_id=2
group by title;
it returns nothing, but it should return some rows of step both user1 and user 2 did.
when i wrote same thing only with user_id=1 or user_id=2, it shows selected information
how can I fix my code so it can show the information I want to get?
thanks in advance :)
user_id cannot be 1 and 2 at the same time. You need a second user table. Then join those on your criteria and count:
select title, count(u1.id) + count(u2.id) as number_of_times
from step u1 join step u2
on u1.id = u2.id
where u1.user_id = 1 and u2.user_id=2
group by title;
note: cannot tell what table title is in, or the purpose of step_taken was as step.id is identical.

Fetching crossed records from database

I have the following table structure (simplified):
id | structure_id | structure_hash_id
1 1 1
2 1 2
3 1 3
4 2 4
5 2 1
6 3 2
As you can see, all structures contain many structure hashes. What I want to fetch is information for each structure id, how many structure hashes it contains exist in other structures. So for this example it'd be:
structure_id #1: 2
structure_id #2: 1
structure_id #3: 1
The query I wrote for this is:
SELECT contains.structure_id, COUNT(contains.structure_hash_id)
FROM (
SELECT *
FROM structureTable st
WHERE structure_id = 1
) AS contains
INNER JOIN (
SELECT *
FROM structureTable st
WHERE structure_id != 1
) AS notcontains
ON contains.structure_hash_id = notcontains.structure_hash_id
GROUP BY contains.structure_id;
It works, I wrote it from memory, I don't remember how I wrote it earlier as I deleted it, but you got the idea.
But the problem is that in real table I've got ~500mln records and some other columns, so for each structure_id the query execution time is huge (> 15min).
Also, I have type structure_id manually, while I'd like to have them all as a result like I gave an example at the top of this post.
How can I solve this problem?
You can achieve this with self join and group by.
Here is the way to do that:
select
t1.structure_id ,
count(t1.structure_id ) as count
from structure t1
inner join structure t2 on t1.structure_id !=t2.structure_id
and t1.structure_hash_id=t2.structure_hash_id
group by t1.structure_id
SQL Fiddle Example: http://sqlfiddle.com/#!9/678bf7/1/0

Cross tabulation. Count one column against another

I have a table with nine columns, I would like to do a order on one column, counting the frequency against another and listing the users that have performed that action and how many times.
The table is called table 1
So I would like to have in the end
tcodetcountuser
abc 9 user1
abc 4 user2
def 3 user1
So user one performed abc 9 times, user 2 4 times, user one also performed def 3 times
You didn't provide the schema of the table containing the data, but your query has to look something like this :
select tcode, count(*) as tcount, user
from table1
group by tcode, user

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)