mysql query for select from two mysql tables - php

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.

Related

search from one sql table and insert into another

I have a table called places which has 2 million records. In these records there is a column called city_name.
In another table I have 2 columns: city_name and city_id.
My aim is to search the table cities with the city_name from the table places and insert the corresponding city_id from the table cities into the table places.
I have tried to use the following:
UPDATE places
INNER JOIN cities USING (city_name)
SET places.city_id = cities.city_id
The problem with this is that it has worked but i am having random city_id's in the city_id field that does not match the city name.
I should also mention that there might not be a corresponding city name in the table cities, so i want to ignore records that do not exist.
Can anyone help please. I am using phpmyadmin
Thank you
If I understand your request correctly the solution would be
UPDATE places
SET places.city_id = (SELECT cities.city_id
FROM cities
WHERE cities.city_name = places.city_name)

how to remove repetition of data in mysql?

This is my detail table:
This is my contact table:
Here are two rows in details table and 4 rows (each details have 2) in contact table. When I use join query I get 4 results row but I want only 2 row(one row of details row with one contact of that corresponding details).
my query:
$this->db->select('*');
$this->db->from('dots_center_detail');
$this->db->join('dots_center_contact', 'dots_center_contact.registration_id = dots_center_detail.registration_id','left');
try this
$this->db->query("
SELECT DISTINCT dots_center_detail.registration_id, dots_center_contact.contact
FROM dots_center_detail
LEFT JOIN dots_center_contact ON dots_center_contact.registration_id = dots_center_detail.registration_id
")
Using select('DISTINCT *') in place of select('*') may give the result you need. It's worth a try.
Or you can create a view like this to go with your table definitions.
CREATE OR REPLACE VIEW dots_center_unique_contact AS
SELECT DISTINCT * FROM dots_center_contact;
Then refer to that dots_center_unique_contact view in your join operation.
Your best bet long term is to figure out why you have duplicate rows, and tighten up your business rules so you don't.

CodeIgniter join select as

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');

Procedure that for looks for an id in different table and inserts it if there is no entry for that id

I need to write a procedure in mysql that runs every night and performs the following:-
For each 'id' from 'associate' table find if there is an entry in 'status table' for a particular 'department' . If there is an entry then move on to next 'id' else insert a row in status table for that id.
Associate Table
Assoc_Id
Assoc_Project
Assoc_Assigned_On
Status Table
Sub_id
Assoc_Id
Sub_on
Department
I just have no clue about how to do this. Any help will be highly appreciated.
Thanks in advance
Since you haven't provided a full schema, I'm unsure of primary/foreign keys on the tables, but here is a query that can help you:
INSERT INTO tbl_status (Assoc_Id, Sub_on, Department)
SELECT a.Assoc_Id, [Your Sub_on Value], 'IT'
FROM tbl_associate a
LEFT JOIN tbl_status s ON a.Assoc_Id = s.Assoc_Id AND s.Department = 'IT'
WHERE s.Sub_Id IS NULL;
The basic idea here is to LEFT JOIN on the status table from the associate table where the Assoc_Id's are equal and the department value is the one you're searching for. The WHERE clause filters the results so that it only shows records that are not currently in the status table based on the join condition. I chose Sub_Id because I assumed that is the primary key. It doesn't really matter what you choose here except that the value has to be a non-null field in the status table order for this query to work.

Data from two tables with same column names

I have a table for users. But when a user makes any changes to their profile, I store them in a temp table until I approve them. The data then is copied over to the live table and deleted from the temp table.
What I want to achieve is that when viewing the data in the admin panel, or in the page where the user can double check before submitting, I want to write a single query that will allow me to fetch the data from both tables where the id in both equals $userid. Then I want to display them a table form, where old value appears in the left column and the new value appears in the right column.
I've found some sql solutions, but I'm not sure how to use them in php to echo the results as the columns in both have the same name.
Adding AS to a column name will allow you to alias it to a different name.
SELECT table1.name AS name1, table2.name AS name2, ...
FROM table1
INNER JOIN table2
ON ...
If you use the AS SQL keyword, you can rename a column just for that query's result.
SELECT
`member.uid`,
`member.column` AS `oldvalue`,
`edit.column` AS `newvalue`
FROM member, edit
WHERE
`member.uid` = $userId AND
`edit.uid` = $userId;
Something along those lines should work for you. Although SQL is not my strong point, so I'm pretty sure that this query would not work as is, even on a table with the correct fields and values.
Here is your required query.
Let suppose you have for example name field in two tables. Table one login and table 2 information. Now
SELECT login.name as LoginName , information.name InofName
FROM login left join information on information.user_id = login.id
Now you can use LoginName and InofName anywhere you need.
Use MySQL JOIN. And you can get all data from 2 tables in one mysql query.
SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1

Categories