2-step SQL Query..something seems wrong - mysql

I'm writing what is a pretty simple 2-step SQL Query.
I have one table called Users and another called ProfileCharacteristics.
**Users Table:**
UserId [PK]
UserName
**ProfileCharacteristics Table:**
UserId [FK]
.....(other data)
I'm trying to get access to (other data), but I only have the UserName available. So what I'm presently doing is running one SQL Query that matches the UserName to the UserId and stores the UserId value.
Then, I'm pulling all values that match to UserId in ProfileCharacteristics in a separate query. I have a gut feeling that I could combine these two queries into one, but I'm not sure how.
Any pointers?
EDIT: The start of a JOIN?
SELECT * FROM ProfileCharacteristics
INNER JOIN Users
ON ....

What you're looking for is an INNER JOIN:
SELECT pc.*
FROM ProfileCharacteristics pc
JOIN Users u ON pc.UserId = u.UserId
WHERE U.UserName = 'someuser'
A Visual Explanation of SQL Joins

Related

learning mysql, JOIN query

i'm a beginner on MYSQL db and i'm trying to play around with the query and relations.
i have created 2 tables, one is 'users' which contain the field staff_ID and the other is 'reports' which also contain the table field staff_ID of the user submitting the reports.
on the relations (see picture) i have connect the 2 staff id field.
every user can submit more than one reports, so i'm try to query and get only the reports of one users(staff_ID).
I understood i have to use the JOIN keyword in order to obtain the data..
i tried the following query but it gave me all the result for all the users.
SELECT u.staff_ID
, u.Name
, r.id_report_show
, r.date_report
FROM users u
JOIN reports r
ON r.staff_ID = u.staff_ID
but I would like to have the report only of one specific user like staff_ID = 04033
probably i understood wrong how this query JOIN work, i'm looking for some help.
Thanks
You are almost there. Your join is perfect. You just need a where clause.
SELECT users.staff_ID, users.Name, reports.id_report_show, reports.date_report
FROM `users` INNER JOIN reports ON reports.staff_ID = users.staff_ID
where users.staff_ID = 04033
Or you can also mention it within on clauses:
SELECT users.staff_ID, users.Name, reports.id_report_show, reports.date_report
FROM `users` INNER JOIN reports
ON reports.staff_ID = users.staff_ID and users.staff_ID = 04033
Since it's inner join both the query will produce same output. But for left join those might produce different result. It's a good practice to use where clause instead of mentioning the condition in on clause.

MySQL subquery returns more than one row selecting count with subquery

Im executing this query
SELECT COUNT(*) AS CONNECTIONS, SERVERS.NAME AS SERVER_NAME, USERS.USERNAME AS USER, CONECT_DATE AS CONNECTION_DATE
FROM CONECTIONS JOIN SERVERS ON(CONECTIONS.SERVERID = SERVERS.ID)
JOIN USERS ON (CONECTIONS.USERID = USERS.ID)
WHERE CONECTIONS.USERID = (SELECT ID FROM USERS
WHERE UPPER(USERNAME) = UPPER((SELECT USERNAME FROM USERS)))
AND CONECT_DATE BETWEEN '2010-06-11' AND '2019-06-11'
GROUP BY SERVERID, CONECT_DATE;
Im trying to get this query from each user in the db TABLE USERS with making a subquery to select everyone 'UPPER((SELECT USERNAME FROM USERS)))' and the thing is that is returning more than one row of the result but if I put directly the USERNAME like 'ADMIN' for example it gives me the results.
You're doing username=select.... A subquery used in an equality test can return only ONE field/row. Since you're going an unfiltered "gimme everything in the users table", you're returning MANY rows of one field each. For that you need an IN match:
SELECT ...
WHERE ... UPPER(username) IN (SELECT USERNAME FROM users)
^^^^---this
SQL doesn't permit what is essentially
WHERE foo=1,2,3,4,5,6
which is why the IN operator works, for comparisons of sets of values.
This is your where clause:
WHERE CONECTIONS.USERID = (SELECT ID
FROM USERS
WHERE UPPER(USERNAME) = UPPER((SELECT USERNAME FROM USERS))
)
This has at least three problems.
The subquery argument to UPPER() might return more than one value, and likely will, unless the USERS table has 0 or 1 rows.
The = to the UPPER() has a similar problem. An in is more appropriate.
The use of = in the outer where clause has a similar problem. An in is more appropriate.
The following fixes these problems:
WHERE CONECTIONS.USERID IN (SELECT ID
FROM USERS
WHERE UPPER(USERNAME) IN (SELECT UPPER(USERNAME) FROM USERS))
)
However, I doubt that will fix your overall query. Your knowledge of SQL seems a bit limited. I would suggest that you ask another question, provide sample data, desired results, and explain what you want to do. That will make it easier for knowledgeable people on this site to point you in the right directions for writing your queries.

New to MySQL Relationships

I'm new to mySQL relastionships and I'm wondering if you can help me out.
This is what I want to do:
Users Table
user_id
user_name
pass_word
permission_id
Permissions Table
p_id
permission_name
permission_type
I want to create a relationship between p_id (permissions table) & permission_id (user table) so when I query the user table it also brings through the corresponding permission name & type?
Is this possible or am I getting it all wrong?
Should I just use joins?
Thanks,
WebDevB
select * from Users u , Permissions p where u.permission_id = p.p_id;
Visual Representation of SQL Joins
You can perform a SELECT query that will return all of this data that you want.
Try this:
SELECT *
FROM users u
INNER JOIN permissions p ON p.p_id = u.permission_id

Cross referencing tables in a mySQL query?

I have two tables. One is a table of users with a unique id field, and the other is a table of data, with a column that holds the user id of whoever generated that piece of data.
I want to do something like SELECT data,genned_by FROM datatable; but I want to replace the results for genned_by with SELECT username FROM users WHERE id = genned_by
So that the results from the query changes the userid into a username that corresponds with the other table.
I did some research and figured INNER JOIN might be what I'm looking for, but I'm left very unsure of how to use it after reading it. Help?
Try to use
SELECT d.data, u.username FROM database d INNER JOIN user u ON u.id=d.genned_by
Hope it helps you
SELECT datatable.data,users.username
FROM datatable, users
WHERE users.id = datatable.genned_by

Convert many to many to one to one (mysql)

We changed database schema and moved a relationship between users/accounts from a 1-1 to a many to many using a join table accounts_users.
So we have
accounts,
users,
accounts_users (user_id and account_id)
Our data is still 1-1, and we have decided to move back. So I need sql to move back:
To Migrate I used:
INSERT INTO accounts_users (account_id,user_id) SELECT id AS account_id, user_id AS user_id FROM accounts
To move back I have tried:
UPDATE
accounts
SET
user_id = ru.user_id
FROM
accounts r, accounts_users ru
ON
r.id = ru.account_id
Update accounts
Set r.user_id = ru.user_id
FROM accounts r, accounts_users ru
WHERE r.id = ru.account_id
SELECT accounts_users.user_id
INTO accounts
FROM accounts_users
INNER JOIN accounts
ON accounts.id = accounts_users.account_id
All of these give a sql error of some sort. Im guessing its because my sql is ambiguous, and I need some sort of select first or min or something like that.
** To be clear, Im sure still have the 1-1 relationship in the data, but I cant figure out the sql to bring the data from the existing tables back into the original tables. What im looking for is some working sql that will take the data from accounts_users and put the user_id into the account table. Thanks, Joel
You could try...
UPDATE accounts
SET user_id = (SELECT user_id
FROM accounts_users
WHERE accounts_users.accounts_id = accounts.accounts_id);
That'll get pretty tedious if you have a lot of columns in accounts_users that have to go back in accounts, though, and won't work if there is any problems with the ids (hence my previous answer). How many columns are there?
If your mapping is 1-1 then just select the first result (you know there is only one)