Get data from table with id from another table - mysql

i have two Tables users_data and users_statistics
users_data:
id money position uid
1 1000 20921 3
2 3000 8742 0
3 2000 23214 3
users_statistics:
id lastname lastlogin
1 Hans 13.05.2200
2 Uwe 10.03.1900
3 Herbert 13.42.2421
Now, i want to SELECT all lastname WHERE uid = 3
My try was
SELECT `lastname` FROM users_statistics
JOIN users_data USING (id)
WHERE `uid` = 3
With this Query he returns me all 3 Rows, but why?
In the second row the uid is 0...
I hop someone can help, thanks in advance.

Sounds weird but now it works...
I only changed the uid to users_data.uid
SELECT `lastname` FROM users_statistics
JOIN users_data USING (id)
WHERE users_data.uid = 3
Now he only gives me the two rows with uid = 3
When someone can explain this, let me know it :D

your query works for me:
http://sqlfiddle.com/#!9/c5025/1
Also tested under local MySQL 5.5.38

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.

SQL insert both specified and selected values

I have two tables shown below:
TableOwner:
UserID Name Initials
1 Peter Pet1
2 Mary Mar1
3 Petra Pet2
TableAsset
AssetID AssetName OwnerUserID
1 Samsung 3
2 Apple 1
3 Huawei 2
Now I want to insert into TableAsset these records:
AssetID AssetName OwnerUserID
4 Doro 2
5 Sony 1
How to use insert query and select query in one step?
You can do something like this in a single query. Passing the parameter will depend on you how you do it.
insert into TableAsset(AssetName, OwnerUserID)
select 'Doro', (select UserID from TableOwner where Initials = 'Mar1')
union all
select 'Sony', (select UserID from TableOwner where Initials = 'Pet1');
THIS ANSWERS THE ORIGINAL VERSION OF THE QUESTION.
You can look them up:
insert into tableAsset(AssetName, OwnerUserID)
select #AssetName, o.UserId
from tableOwner o
where o.initials = #Initials;
This basic structure will work for any database, although the method for passing parameters may differ among databases.

Join from two tables 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

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)

MySQL - Duplicate elimination and Preserving Valuable Data?

Scenario : I have few duplicate contacts in a table. The duplicates are identified, I can just delete them but the problem is I don't want to lose the data the duplicate might have and the original don't. Any tips?
Sample data :
ID Name Email School Dupe_Flag Key
1 AAA a#a X 1
2 AAB JKL 1
3 BBB b#b MNO X 2
4 BBC 2
Desired output :
ID Name Email School Dupe_Flag Key
1 AAA a#a X 1
2 AAB a#a JKL 1
3 BBB b#b MNO X 2
4 BBC b#b MNO 2
How are 2 records related? : They both have the same Key Value with only one column having the Dupe_Flag SET which is the duplicate column.
In the above case ID 1 is going to be deleted but email info from ID 1 should be applied to ID 2.
What is the Data? : I have few hundred rows and few 100 duplicates. UPDATE statement for each row is cumbersome and is not feasible.
Business rules for determining what data takes priority :
If a column from the original/good record (Dupe_Flag is NOT set) has no data and if the corresponding Dupe record (has the same Key value) column has data then that original record column should be updated.
Any help/script is really appreciated! Thanks guys :)
Assuming empty values are null, something like this should output the desired data:
SELECT
a.ID,
IF(a.DupeFlag IS NULL, IF(a.Name IS NULL, b.Name, a.Name), a.Name) AS Name,
IF(a.DupeFlag IS NULL, IF(a.Email IS NULL, b.Email, a.Email), a.Email) AS Email,
IF(a.DupeFlag IS NULL, IF(a.School IS NULL, b.School, a.School), a.School) as School,
a.DupeFlag,
a.key
FROM
table a,
table b
WHERE
a.Key = b.Key AND
a.ID != b.ID
GROUP BY
a.ID
Note that turning this in an UPDATE statement is pretty straight-forward
I don't know the specifics of this problem but it is probably better to avoid this problem by setting the columns to "unique" so if a query tries to create a duplicate it will fail. I think the elegant solution to this problem is to avoid it at the point of data entry.
I like using this query for tracking down dupes:
select * from table group by `Email` having count(Email) > 1
While this uses a bunch of nested SELECTS, and isn't really a full solution, it should either spark something else, or possibly push in the right direction.
select * from
(select r1.ID,r1.Name,coalesce(r1.Email,r2.Email) as Email,
coalesce(r1.School,r2.School) as School,r1.Dupe_Flag,r1.Key from
(select * from test1 where Dupe_Flag IS NULL) as r1 left outer join
(select * from test1 where Dupe_Flag IS NOT NULL) as r2 on r1.KEY=r2.Key)
as results
Yields:
ID Name Email School Dupe_Flag Key
2 AAB a#a JKL NULL 1
4 BBC b#b MNO NULL 2
Based on your example data.
The rows are unique, so there's no problem. Please recheck your example data.