I have 2 tables in my database which I need to join.
1 table is the artikelen table and the other one is the collecties table. I currently have.
$this->db->select('*');
$this->db->from('collecties');
$this->db->join('artikelen', 'artikelen.collecties_id = collecties.id');
It gives the right result but all the double fields (collecties has a title field and artikelen has a title field) will become one (it returns the artikelen.title field), and I can't access the row of the other table (the collecties.title field).
I select 10 fields from artikelen and only collecties.title from collecties.
What is the simples way to do this without having to replace
$this->db->select('*');
with all the 10 fields with an as statement.
Make sure your both table got rows on your joining condition , otherwise it will return null. and modify the select as follows
$this->db->select('artikelen.*,collecties.title as ctitle');
Related
Actually I am inserting two multiple table data into one table, I have written following query where I wanted to insert some fields from both table into the new table. I wrote the code for newly submitted form for my website but I wanted to update existing data in database.
Please have a look on below qyery.
Please suggest.
below query is giving me an error:
"Operand should contain 1 column(s) "
INSERT INTO am_intranet_hr_lead_document(doc_name,careers_submissions_key,hr_phase_key,response_id,date_added)
SELECT (am_career_submissions.resume,am_career_submissions.careers_submissions_key,am_intranet_hr_lead_response.hr_phase_key,am_intranet_hr_lead_response.response_id,am_career_submissions.add_dt)
FROM am_career_submissions, am_intranet_hr_lead_response
WHERE am_career_submissions.careers_submissions_key = am_intranet_hr_lead_response.careers_submissions_key
Thank You.
Do with inner join and limit
Like this
INSERT INTO am_intranet_hr_lead_document(doc_name,careers_submissions_key,hr_phase_key,response_id,date_added)
SELECT (am_career_submissions.resume,am_career_submissions.careers_submissions_key,am_intranet_hr_lead_response.hr_phase_key,am_intranet_hr_lead_response.response_id,am_career_submissions.add_dt)
FROM am_career_submissions,
join am_intranet_hr_lead_response
on am_career_submissions.careers_submissions_key = am_intranet_hr_lead_response.careers_submissions_key
limit 1
I have this SQL query which is something like this in normal sql syntax
SELECT *
FROM question
LEFT JOIN abcd_selection ON question.questionID = abcd_selection.questionID
WHERE question.SurveyID =21
This works perfectly fine I get what i wanted. However when I switch this over to CI, it does not work strangely. The questionID column disappears for rows that do not have a match with abcd_selection.
$this->db->select('*');
$this->db->from('question');
$this->db->join('abcd_selection','question.QuestionID = abcd_selection.QuestionID', 'left');
$this->db->where('question.SurveyID', $input_qid);
$query = $this->db->get();
Can anyone solve this??
When joining 2 tables, which have the same column name, you will get a NULL value when there is not a row that matches in the second table.
question.SurveyID question.QuestionID abcd_selection.QuestionID
1 2 2 //matching row in abcd_selection
2 3 NULL //no matching row in abcd_selection
Since the column names are the same, php will select the last instance of QuestionID, which will be NULL when a matching row does not exist.
One way to work around this is to select the column as an alias
$this->db->select('*,question.QuestionID as QID');
Now you can select $query['QID'] even when a abcd_selection.QuestionID does not exist.
I have a field that is a varchar that contain values such as (88,90,100,200) and i have another one contains the value 200 when using the IN clause to see if the second field in the first field it returns empty result but when comparing it with 88 it return results
So i was wondering what im doing wrong here and if there is a better way.
Here is the mysql code
select * from user inner join category where parent_id IN (categories)
parent_id is located in the category table and the categories in the user table
When you do a JOIN you need an ON clause to specify that you are looking for rows that somehow "match" across two tables. If you want all the rows from a single table where some column has a value that is "one of the values in this list..." you use IN. So these are both valid SQL:
SELECT * FROM user INNER JOIN category ON user.someColumn = category.someOtherColumn // can add WHERE clause
or
SELECT * FROM user WHERE parent_id IN (88, 99, 100, 200)
Can't tell you exactly what query you should use unless you share your table structures, as the question is unclear.
I'm using codeigniter.
We have two mysql tables.
What I'm doing is taking all the details in the first table and feeding it to an html page to let all users to view it. But now I want to add some details from a second table related to a column in first table.
I mean, I have a column in first table call "Pending_on" in that column I have inserted some branch names
and in the second table I have inserted the contact details of those departments.
So, if we think in the first table fifth row pending_on column has the value "HR" then I want to display the telephone number of the HR department.
So, how can I write a query for this?
For your case, here is an example:
There are 2 tables named entry and subscription.
entry
field1 publisherId
field2 entry
subscription
field1 myId
field2 friendId
You can use a JOIN. In plain SQL:
SELECT e.publisherId, e.entry
FROM entry e
JOIN subscription s
ON s.friendId = e.publisherId
WHERE s.myId = 1234
this is the query i used to do my work
and thanks goes to MR. Yuvvaraj Bathrabagu
$this->db->select('*');
$this->db->from('new_users');
$this->db->join('contact', 'new_users.pending_on = contact.dep','left');
$query = $this->db->get();
$data = $query->result_array();
Let's take your example. You have a column name pending_on in your first table, and in your second table you have your department_name column. Find the below code for joining those two tables,
$this->db->select('firsttable.*,secondtable.contact_details');
$this->db->from('firsttable');
$this->db->join('secondtable', 'firsttable.pending_on = secondtable.department_name','left');
$query = $this->db->get();
So, The department_name in second table and pending_on in first table should have the same value as you mentioned "HR". And my suggestion is to have the id's of the table as reference instead of department names. Hope This helps.
Here is my php/MySQL task:
I have a table POSTS that contains num field that is the primary key and other information fields about the post (author, title, etc.). I also have a table LIKE that contains a userId field that is the primary key and a field POST that corresponds to the num field in posts. Given a specific userID, I need to get all of the rows from the POSTS table that the userId 'likes'.
Table 1 - posts
-num
-author
-title
Table 2 - likes
-userId
-postId
This is all in php so my first idea was to get all of the rows from the LIKES table where the userId matches the one given and store those rows in an array. Then I would iterate through the array and for each row I would search get the row of the POSTS table where postId=POSTS.num. However, this seems like it would be rather slow, especially since each iteration through the array would be a separate mysql query.
I am assuming there is a faster way. Would it be to use a temporary table or is there a better way to join the tables? I have to assume that both tables contain many rows. I am a mysql novice so if there is a better solution please explain why it is better. Thank you in advance for you help!
Try the following query:
SELECT
`posts`.*
FROM
`likes`
INNER JOIN
`posts` ON
`posts`.num = `likes`.postId
WHERE
`likes`.Userid = {insert user id here}
Depending on your schema (not sure if each record in 'likes' has to be unique, you may want to use the DISTINCT keyword on your select to filter out duplicates.
SELECT poli.* FROM (
SELECT po.* FROM posts po
JOIN likes li
ON li.postId = po.num
WHERE li.userId = '$yourGivenUserId'
) AS poli
$yourGivenUserId is the given userId.