Easiest way to fetch data where userid matches the userid from another table [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Easiest way to fetch data where userid matches the userid from another table [closed]



So i have a table called job_opslag, where company's job posts is stored.


job_opslag



I also have a table called virksomheder, where the company's information is stored (address etc).


virksomheder



So I want to fetch the data from virksomheder where userid matches the job_opslag's userid, and then print it out.


virksomheder


userid


job_opslag



What is the easiest way to do that?



Kind regards



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Welcome. To ask On Topic question, please read Question Check list and the perfect question and how to create a Minimal, Complete and Verifiable Example and take the tour We are very willing to help you fix your code, but we dont write code for you
– RiggsFolly
Aug 10 at 9:28





@RiggsFolly I'm not asking anyone to write code for me. I'm asking for clearing an doubt, since i'm lost there. Thank's :-)
– 2accbot
Aug 10 at 9:29





Do you know SQL Join?
– Ronnie Oosting
Aug 10 at 9:29





For future reference, the way to get a good response from SO is to at least show what you have tried, otherwise it looks like a Do It For me question
– RiggsFolly
Aug 10 at 9:30





Well, that is your answer. Do you need an example? And can you provide us your current query?
– Ronnie Oosting
Aug 10 at 9:31




7 Answers
7



The SQL you can do it with is the following:


select * from job_opslag j, virksomheder v
where j.userid = v.userid;





Would have been a much better attempt at an answer if you had used a JOIN
– RiggsFolly
Aug 10 at 9:33



You can use join query


select * from job_opslag j JOIN virksomheder v ON j.userid=v.userid;





maybe both tables use primary key as "id" so "select *" is not a good option
– Rajinder
Aug 10 at 9:43





yes but still if user want to add particular column he can add right instead of * ?because he didn't mention which columns he want to display from both tables
– suresh
Aug 10 at 10:16



Your query should look something like:


SELECT *
FROM job_opslag jo
LEFT JOIN virksomheder AS v ON jo.id = v.id



What you do to "echo that out" depends entirely on what you're doing and where you're calling it from.



You can acquire this by using a SQL Join.



Example:


SELECT something
FROM first
LEFT JOIN second ON first.column_name = second.column_name;
// Now add your WHERE etc.



A beautiful example can be found here:



Documentation:



Grab something to smoke/drink/eat and a couple of hours of your time. Once you get your hand into 'JOIN's your live will be much more fun. Joins are very use-/ and powerful. Related to the future once your skills improve, your database's and code are somewhat 'next level' expanded you might want to create a relational database.


relational database



Please search google on 'Relational Database MySQL', if you want to learn more about it.



use can try something like:


SELECT job_opslag.[Field_names], virksomheder.[Field_names] FROM job_opslag
LEFT JOIN virksomheder ON virksomheder.[Your Foreign Key] = job_opslag.[Your Promary Key];



also, use can change Join type such as RIGHT JOIN or INNER JOIN according to your data flow





don't forget to add the comment if the answer is not useful. if you just mark down vote without comment then don't do it
– Rajinder
Aug 10 at 9:44



You can just use SQL join
to get information from both the tables.


select * from job_opslag a inner join virksomheder b on a.userid = b.userid



(If for every userid in job_opslag(a), there is a userid in virksomheder(b)
or if you want only those records which have userid in both the tables)



You can use left join when every userid in table a might not be present in table b or vice versa


Select * from job_opslag a left join virksomheder b on a.userid = b.userid



(Here you will be all the records from table a even if there are no matches for the userid in table b)





What does this add to the answer set that has not already been given as answers
– RiggsFolly
Aug 10 at 10:04



You have to pass your required input user id here $User_id
and write the following query, It will join those two tables and fetch the data.



mysqli_query() will prepare and execute the query , mysqli_fetch_array() will fetch the data from the executed query in array format. Here $Conn refers to db connection.


$sql = mysqli_query($Conn,"select * FROM job_opslag a left join virksomheder b on b.userid = a.userid where a.userid =". $User_id);

$Data = mysqli_fetch_array($sql,MYSQLI_ASSOC);





mysql_ Really! You really should be using PHP7, or 7.1 or 7.2 where this api is no longer supported
– RiggsFolly
Aug 10 at 9:40


mysql_





only use PDO or mysqli to secure the db queries
– Rajinder
Aug 10 at 9:41

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard