i need to make a query on a table and inside this queryi would like to replace the value of "created_by" in the value of corsspende user "first_name"
becaus a user create another user ,so i would like return the real name of creater instead of his id
i tried to get deeper in queries typess but the are so many ,inneer join, outer ,left join ,variable ,..
i don,t know which concept is the solution [enter image description here]
You'll perform an INNER JOIN from this table back to itself:
SELECT users.*, createdby.*
FROM yourtable users
INNER JOIN yourtable createdby
ON users.created_by = createdby.id
In order to do this the each table in the FROM clause gains an alias. The first time we use it I'm calling it users and the second time I'm calling it createdby. This makes it obvious in the rest of the SQL which table is being referred to and why.
We join on the created_by of the first table to the id of the second. Essentially the second table contains the attributes of the created_by user.
You'll just have to swap the yourtable out for your actual table name and probably swap those * out with the actual fields you want from each table.
This should be handled by client side code instead of query because it causes confusion. If you really want it, you can just take first_name column value and return it as create_by like showed below:
SELECT first_name AS create_by FROM user;
Related
I've currently got a generic Users table with the following columns
user_id
first_name
last_name
join_date
last_active
email
password
rank
status
Some users (the ones with rank = 3) will provide some extra information like telephone number, secondary email, birthday, etc (total 15 fields). I was wondering if I should add these columns on this table or if I should create another table and connect with the Users one with user_id. Something like this:
user_id
all new columns
Which one would be the best option?
The two table approach is viable.
To get all columns from both tables:
SELECT t1.*, t2.*
FROM t1
LEFT JOIN t2 USING(user_id);
I as assuming that the row in t2 will be sometimes missing, yet you want to get the row, with NULLs for the missing data -- hence LEFT JOIN.
Of course, if you know that you won't need t2, don't do the JOIN. But there is no harm, other than a little performance, in always doing the JOIN.
Is it the "best option"? That is hard to say without knowing more about your system. But, based on what information you mention, it seems like a 'good' option.
There is a table in our database that contains customer information including their first and last name. The first and last name are stored as separate fields and not together as one name. There is also a table which stores a referral field. In this field, someone can place the name of the customer that referred them to our services.
I would like to utilize a query that will take the referral field (which would contain the name of a prior customer) and match it up to the record to that prior customer.
I thought the below would work:
SELECT APPLICATION_ID
FROM APPLICATION_TABLE
JOIN APPU_USER ON APPU_APPLICATION_ID = APPLICATION_ID
LEFT JOIN APBD_APP_BASIC_DATA ON APBD_APPLICATION_ID = APPLICATION_ID
WHERE CONCAT(APPU_FIRST_NAME,' ',APPU_LAST_NAME) = APBD_REFERRAL_STRING;
What do I need to utilize to be able to do this?
everything looks fine in your query. Is a good practice to put the table names when you use two or more tables in a query to avoid same fields conflicts, something like:
LEFT JOIN APBD_APP_BASIC_DATA ON APBD_APP_BASIC_DATA.APBD_APPLICATION_ID = APPLICATION_TABLE.APPLICATION_ID
also, take in mind than
CONCAT(APPU_FIRST_NAME,' ',APPU_LAST_NAME) = APBD_REFERRAL_STRING;
can cause problems if referral string is in format last name,first name or first name, last name, or with 2 spaces
Here is my Database structure (basic relations):
I'm attempting to formulate a one-line query that will populate the clients_ID, Job_id, tech_id, & Part_id and return back all the work orders present. Nothing more nothing less.
Thus far I've struggled to generate this Query:
SELECT cli.client_name, tech.tech_name, job.Job_Name, w.wo_id, w.time_started, w.part_id, w.job_id, w.tech_id, w.clients_id, part.Part_name
FROM work_orders as w, technicians as tech, clients as cli, job_types as job, parts_list as part
LEFT JOIN technicians as techy ON tech_id = techy.tech_name
LEFT JOIN parts_list party ON part.part_id = party.Part_Name
LEFT JOIN job_types joby ON job_id = joby.Job_Name
LEFT JOIN clients cliy ON clients_id = cliy.client_name
Apparently, once all the joining happens it does not even populate the correct foreign key values according to their reference.
[some values came out as the actual foreign key id, not even
corresponding value.]
It just goes on about 20-30 times depending on largest row of a table that I have (one of the above).
I only have two work orders created, So ideally it should return just TWO Records, and columns, and fields with correct information. What could I be doing wrong? Haven't been with MySQL too long but am learning as much as I can.
Your join conditions are wrong. Join on tech_id = tech_id, not tech_id = tech_name. Looks like you do this for all your joins, so they all need to be fixed.
I really don't follow the text of your question, so I am basing my answer solely on your query.
Edit
Replying to your comment here. You said you want to "load up" the tech name column. I assume you mean you want tech name to be part of your result set.
The SELECT part of the query is what determines the columns that are in the result set. As long as the table where the column lives is referenced in the FROM/JOIN clauses, you can SELECT any column from that table.
Think of a JOIN statement as a way to "look up" a value in one table based on a value in another table. This is a very simplified definition, but it's a good way to start thinking about it. You want tech name in your result set, so you look it up in the Technicians table, which is where it lives. However, you want to look it up by a value that you have in the Work Orders table. The key (which is actually called a foreign key) that you have in the Work Orders table that relates it to the Technicians table is the tech_id. You use the tech_id to look up the related row in the Technicians table, and by doing so can include any column in that table in your result set.
I'm so lost here I don't even know how to best title my question.
I am creating a simple dating site. I want the women to be able to block the men just like all other dating sites. When this happens, I don't want the womens' profiles to be returned in a query.
Table A
Members table containing all the profile information including a member name
Table B
Blocked members table containing the woman's name and the man's name for each case in which the woman has blocked a man
So, I want something like this:
$query = Return all records from table A where sex=female and there is no record in table B containing the woman's name and the man's name
I thought I would run a query against table B to retrieve all women who have blocked me, then run a query against table A to return all females in which the woman's username is NOT contained in the results of my first query. However, I can't figure out how to do this.
If I understand your question...seems like a simple join, no? Not sure if I'm misunderstanding. Something like this perhaps:
SELECT * FROM Table1 WHERE Table1.ID NOT IN (SELECT BLOCK_ID FROM table2)
So Table1 has all ID's of the women, and Table2 has all block id's (for example) and you want what is not in that? Obviously some changes required on top of this.
If you wanted to see a list of all the female members who had blocked the current user, you would use a query like:
SELECT member.*
FROM TableA member
JOIN TableB blocked ON (member.name = blocked.user_who_blocked)
WHERE member.sex = female
AND blocked.blocked_user_name = 'Joe McCurrentUser'
;
So, if you want to see the set of users where that is not the case, you use a LEFT JOIN and look for a null id.
SELECT member.*
FROM TableA member
LEFT JOIN TableB blocked ON (member.name = blocked.user_who_blocked)
WHERE member.sex = female
AND blocked.blocked_user_name = 'Joe McCurrentUser'
AND blocked.id IS NULL
;
You can modify as you wish to use the actual columns in your tables. Make sure you have indices on both the user_who_blocked and blocked_user_name columns in that table.
Would this work?
Select * from Table A
inner join Table B on a.womans_name = B.womans_name and B.mans_name="Mans Name"
where B.womans_name IS NULL
If Table B contains a record with the matching womans_name and mans_name then the join will create one record containing all the fields in Table A and Table B but the Where clause will reject this record because the womans_name from Table B will not be null. If Table B does not contain a matching record then all those fields will be null (including B.womans_name) so the Where clause is satisfied.
I am having a text-box which is auto complete.
When i enter the name on the text-box the auto complete will work and provide the list from table.
And when i click the save button the names which are entered in the text-box will inserted in a table. Until this, the process i working fine.
My case is : Already existing users should not show in auto-complete.
Table Structure :
Users Table
uid, name
work table
wid, from_uid, to_uid,
How the mysql query should be.. Any help regarding this will be thankful and gratedful...
select name
from users
where name like 'Jo%'
and uid not in (select to_uid
from work
where from_uid = 666)
Supposing that user entered Jo substring and current user's id is 666
or another query that does the same (not sure which one will be more efficient in your particular case) but uses LEFT JOIN instead of subquery:
select name
from users u
left join work w on w.from_uid = 666
and u.uid = w.to_uid
where name like 'Jo%'
and w.to_uid is null