mysql write select subquery on same table with condition on the resultset - mysql

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

Related

Finding Values of same ID from different table

I have 2 tables:
1.Users (This table contains all the information of users like name, Userid, mobileno)
2.Transaction (This table contains the information of all the transaction of a user)
But the UserID is same in both the tables
I have some filter conditions like:
[ TransactionType=1 AND status=1 and (RealCash>0 or Bonus>0 or Winning>0)] which i want to apply on Transaction table
once I applied the condition i will have some UserID
Now i want that the information of the users from the Users table that have the same UserID which i've obtained from above from the transaction table
How can i do that in MYSQL ?
use JOIN : https://www.mysqltutorial.org/mysql-join/
For example:
SELECT
u.name,
u.Userid,
u.mobileno,
t.TransactionType
FROM
Users u
INNER JOIN Transaction t ON t.Userid = c.Userid
WHERE t.TransactionType=1 AND t.status=1 and (t.RealCash>0 or t.Bonus>0 or t.Winning>0)
But read carefully about other join types (left, right, cross) as you may get different results.
SELECT
name, Userid, mobileno
FROM
Users
WHERE
UserID IN (SELECT
UserID
FROM
Transaction
WHERE
TransactionType=1 AND status=1 and (RealCash>0 or Bonus>0 or Winning>0);

i am trying to understand what does this query means?

select user.*,
store.storeId,store.storeName from
user JOIN store ON user.storeId=store.storeId
where email=? and password=?
This is my mysql query thought of when joining the table but i am not able to get what my query is doing i am unable to get that.
Select everything from the user table.
select user.*,
Select storeId and storeName from the store table.
store.storeId,store.storeName
join the 2 tables together where the storeId from user table is = to storeId in the store table where the email and pass = what they typed in the input
from
user JOIN store ON user.storeId=store.storeId
where email=? and password=?

Joining tables in MySQL and requesting data from second table only

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

MySQL select rows that do not have matching column in other table

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

How do I get the values from one table based on the relationship specified in another?

I have a table called user_info which has a unique id and general information like title, firstname, lastname, number, etc..
I have another table useralias which has two columns; userid and aliasid.
At the moment I select * from user_info where timestamp > 'a timestamp'.
How can I also append to the results a duplicate of each user_info record for each useralias record in the alias table based on userid, keeping into account the timestamp.
Except I want to replace the userid with the alias id for these extra records, but return the rest of the details as if there were an exact copy of that user_info record except with a different id.
If you just want the aliases and not the original user id, then a simple join does what you want:
select ua.aliasid, <all columns from user that you want>
from user u join
useralias ua
on u.userid = ua.userid
If you want both the userids and the aliases, then you need to augment the table you are joining to:
select ua.aliasid, <all columns from user that you want>
from user u join
(select userid, aliasid
from ((select userid, aliasid
from useralias ua
)
union all
(select userid, userid as aliasid
from user
)
) t
) ua
on u.userid = ua.userid
This query creates a list of pairs of user ids and alias ids and also adds in all existing user ids. When you join, you'll get the original records. You could also do this with two separate queries connected by a join.