I'm trying to join 2 tables where I need to show only 3 columns from the second one where another column is used as a comparison.
For example:
Table one is called employee: it has a column called user_id and some other columns
Table two is called people: it has a column called user_id which included some of the employees user_ids
The columns I want to select are all from table people! (firstname, lastname, email)
I tried the following but something going wrong:
SELECT userid, firstname, lastname, email
FROM people
JOIN employee
WHERE people.userid = employee.userid;
I'm not sure what am I doing wrong, could you please help me correct it?
You can try this query:
SELECT
p.userid,
p.firstname,
p.lastname,
p.email
FROM
people as p,
employee as emp
WHERE
p.userid = emp.userid
Looking at your script, it looks like you'll run into ambiguous columns in at least your userid. You want to explicitly tell SQL where the column comes from like in your WHERE clause if there are columns sharing the same name between the two tables.
SELECT
userid, -- AMBIGUOUS
firstname,
lastname,
email
FROM people
JOIN employee
WHERE people.userid = employee.userid;
Example solution:
SELECT
people.userid,
people.firstname,
people.lastname,
people.email
FROM people
JOIN employee
WHERE people.userid = employee.userid;
For this issue you can use this query
let suppose that I have a users table where a user have zero to one profile picture
I need the user (Name,LastName,BirthDate) for users who have no profile picture
I can use this query
select *
from user c
where NOT EXISTS (
select 1
from photo p
where p.id = c.photo_id
)
in this where you can use any field between this two table
removing the not will result on the users who have a profile picture
hope this help you
you can search for SEMI JOIN and ANTI JOIN for more informations
i think this query will solve your problem
insert into table1 (clmn_1,clmn_2,clmn_3) SELECT clmn_1,clmn_2,clmn_3 FROM table2 where id = value
Related
I want to get all the data from users table with createdBy populated with name matching userid.
means
instead of showing createdBy column with id, I want to show createdBy
name querying by userid. Is there any possibility. Please help me.
table: User
This is what i get
This is what i need
Use self join
demo
select a.*,b.name as createdbyname from user a left join user b
on b.id=a.createdby
This should do the trick. You join the table with itself (actually, with only the userid and names fields, to avoid conflicts with the original createdBy field) on the matching userids, then select only the relevant fields
SELECT u.userId, u.name, sj.createdBy
FROM users u JOIN (SELECT userId, name AS createdBy FROM users) AS sj
ON u.createdBy = sj.userId
Perhaps it's too complicated to write the problem so I will give you all the picture on how I want the final table looks like:
As you can see there are 3 tables,
1st and 2nd table are similar (have username and email)
but unfortunately not all username have email
but at least I can fix some username
and be able to join all the username without email in one table (like in 3rd table)
How can I do this in sql or using query?
I tried UNION but it repeat the "a" and "b".
This should give you the results you're looking for:
Select Coalesce(A.UserName, B.UserName) As UserName,
Coalesce(A.Email, B.Email) As Email
From TableA A
Left Join TableB B On B.UserName = A.UserName
Union
Select Coalesce(A.UserName, B.UserName) As UserName,
Coalesce(A.Email, B.Email) As Email
From TableA A
Right Join TableB B On B.UserName = A.UserName
Provided username is unique in both tables, you need FULL JOIN them. And as MySql has no FULL JOIN, you need to mimic it, see https://dba.stackexchange.com/questions/101549/how-to-mimic-a-full-outer-join-using-mysql-views
I have a table in a MySQL DB, called ‘users’. The fields for users are : id, email, username, first_name, last_name. Another table in the same MySQL DB, called ‘premium_users’ , contains 2 fields : user_id, premium_service_id. A third table called ‘premium_services’ contains 2 fields : premium_service_id , premium_service_name.
I want to create an SQL query , to interrogate my db, so i can have a full list of what premium services has every premium user. How can i interrogate properly with inner join? I’ve try this :
select * from users inner join premium_users on users.id = premium_users.user_id inner join premium_services on premium_users.premium_service_id = premium_services.premium_service_id;
Since you say which service has every user, you'll need to use aggregation to determine this. Here's one way:
select user_id
from premium_users
group by user_id
having count(*) = (select count(*) from premium_services)
SQL Fiddle Demo
Depending on your data, you may need count(distinct premium_service_id) instead, but you should have constraints that don't allow duplicates in those table.
Rereading your question, I might have got this backwards. Looks like you want a list of premium services instead of users. Same concept applies:
select ps.premium_service_id
from premium_services ps
join premium_users pu on ps.premium_service_id = pu.premium_service_id
group by ps.premium_service_id
having count(distinct pu.user_id) = (select count(distinct user_Id) from premium_users)
More Fiddle
I can't seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets assume we have a users table and a sent table.
users table has the following columns: id, username
sent table has the following columns: id, username
I want to select all rows from users where username does not exist in sent table. So, if tom is in users and in sent he will not be selected. If he is in users but not in sent he will be selected. I tried this but it didn't work at all:
SELECT pooltest.name,senttest.sentname
FROM pooltest,senttest
WHERE pooltest.name != senttest.sentname
Typically, you would use NOT EXISTS for this type of query
SELECT p.Name
FROM pooltest p
WHERE NOT EXISTS (SELECT s.Name
FROM senttest s
WHERE s.Name = p.Name)
An alternative would be to use a LEFT OUTER JOIN and check for NULL
SELECT p.Name
FROM pooltest p
LEFT OUTER JOIN senttest s ON s.Name = p.Name
WHERE s.Name IS NULL
Note that the implicit join syntax you are using is considered obsolete and should be replaced with an explicit join.
Try this SQL:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.username = users.username
WHERE sent.username IS NULL;
The better way in my opinion would be:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.id = users.id
WHERE sent.id IS NULL;
As both the id fields, would be indexed (primary key I would have thought) so this query would be better optimised than the first one I suggested.
However you may find my first suggestion better for you, it depends on what your requirements are for your application.
May be this one can help you ....
I had also the same problem but Solved using this this query
INSERT INTO tbl1 (id,name) SELECT id,name from tbl2 where (name) not in(select name from tbl1);
hope this one will solve your problem
I have two tables in a MySQL Database.
Users table and Users Meta Table
I am looking for a way to get all the information out of both tables with one query. But without repeating the information from Users table.
This is all information relating to the users id number as well. So for example user_id = 1.
Is there a way to query the database and collect all the information I from both tables without repeating the information from the first?
Structure Example
Users Table
user_id
user_login
user_pass
Users Meta Table
user_meta_id
user_id
user_meta_key
user_meta_value
Im wanting to get out of this
user_id, user_login, user_pass, user_meta_id, user_id, user_meta_key, user_meta_value
user_meta_id, user_id, user_meta_key, user_meta_value
user_meta_id, user_id, user_meta_key, user_meta_value
Sure, that's easy, just specify the fields you want from each table in the query with a join and don't include the columms that are repeated.
SELECT Users.field1, Users.field2, Users.field3, Users.user_ID,
UsersMeta.field4, UsersMeta.field5
FROM USERS
LEFT JOIN UsersMeta ON (Usuers.user_ID=UsersMeta.User_ID)
SELECT DISTINCT table1.id, table1.field2, table1.field3, tab_id.id_table1
FROM table1
LEFT JOIN tab_id ON (table1.id=tab_id.id_table1)
yes, it is possible using DISTINCT keyword in query
SELECT DISTINCT Users.field1, DISTINCT users.field2, UsersMeta.field3
FROM USERS, UsersMeta
WHERE Users.user_ID=UsersMeta.User_ID
Distinct description
You didn't mention what database server you are using.
Assuming your tables are like:
USERS (user_id, first_name, last_name, gender)
USER_META (user_id, comment_count, likes)
Your query would look like this in MySQL:
SELECT u.user_id, first_name, last_name, gender, comment_count, likes
FROM USERS u LEFT JOIN USER_META m ON (u.user_id = m.user_id);
That's the work of front-end tools/language, e.g. Crystal Report, PHP, C#, etc. Don't do it in query