I am query a database to get all the employers names out of my database, but I only want to get the ones where their ID is present in my jobs table, here is what I am trying to do.
$this->db->select('*')
->from('employers')
->join('jobwall', 'jobwall.employers_employer_id = employers.employer_id', 'left');
However this does not return the correct results, how can I select all my employers from the employers table but only if they have data in the jobwall table?
You need to add a WHERE clause:
$sql = '
SELECT *
FROM employers
LEFT JOIN jobwall ON jobwall.employers_employer_id = employers.employer_id
WHERE employers.employer_id
IN (SELECT employers_employer_id FROM jobwall)
';
$this->db->query($sql);
I'm not sure how complicated this would be to create using Codeigniter's activerecord class.
I think a better WHERE clause might be:
SELECT *
FROM employers
LEFT JOIN jobwall ON jobwall.employers_employer_id = employers.employer_id
WHERE jobwall.id IS NOT NULL
This will exclude any rows that don't have a corresponding job.
You should use whatever Primary Key your jobwall table has if jobwall.id doesn't exist.
This can also be written using ActiveRecord easily.
I don't understand you exactly, but I think you should use join without left:
this->db->select('*')->from('employers')->join('jobwall', 'jobwall.employers_employer_id = employers.employer_id');
Related
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.
I want to join student_image table with student_details table where
image_id = student_id
but Im stuck here, the query gives the student details, but now I want to add the student image to be displayed together with the student
Below is my query
$query = $this->db->get_where('student_detail',array('reg_no'=>$reg_no))->result_array();
Are you using CodeIgniter?
If you want to display student_image also you can do it like this:
$this->db->select('*');
$this->db->from('student_image a');
$this->db->join('student_details b', 'a.image_id=b.student_detail_id','inner');
$this->db->where('b.student_detail',$reg_no);
return $this->db->get();
or you can do like it this:
$query = "select * from student_image as a join student_details as b on a.image_id=b.student_detail_id where b.student_detail=?"
return $this->db->query($query ,array($reg_no));
I hope this solves your problem
Myself, and probably anyone else has absolutely no idea what that db class you're using is, so we cannot help you with that detail.
A query like this is very basic. A plain query would look somewhat like this:
SELECT *
FROM student_details, student_image
WHERE student_details.studentID = student_image.studentID
And that's it. Now you have to translate it into working with your db class...
I would recommend you learn basic mysql queries before you start using special pre-made classes. They make things a lot more complicated when you do not know what you want it to do yet.
-- The above query will get you the student details only if the row actually exists in both tables.
If you want it back even if it doesn't exist, you could use a LEFT JOIN, like so:
SELECT *
FROM student_details
LEFT JOIN student_image ON student_details.ID = student_image.ID
WHERE student_details.ID = 'myStudentID'
The above query would retrieve a student's results.
I am trying to form a query for MySQL where it gets all the info from multiple tables but only displays the ones where the "activity" = "Other". Right now it is displaying everyones info and I don't know the proper way to format the WHERE part of the query. I want it to access the jobSearch table, read the activity and only return the ones where the activity is "Other"
$query_student = " SELECT *
FROM student
JOIN major
ON student.studentID=major.studentID
JOIN jobSearch
ON major.studentID=jobSearch.studentID
WHERE jobSearch.activity == Other";
You SQL syntax is wrong, it should be:
SELECT * FROM table_name WHERE column = value
In your case:
SELECT * FROM student ... WHERE jobSearch.activity = 'Other'
For reference, check this good tutorial about SQL syntax: http://www.tutorialspoint.com/sql/sql-where-clause.htm
Your SQL syntax is OK is not wrong
SELECT *
FROM student
JOIN major ON student.studentID=major.studentID
JOIN jobSearch ON major.studentID=jobSearch.studentID
WHERE jobSearch.activity = 'Other';
If jobsearch.activity field is a varchar use single quotes around the word Other and use only one "=".
Also I recommend using alias and not using "Select *", you might run into a problem because you have the same field name in different tables, try to use something like this.
SELECT st.*, mj.field1, mj.field2, js.activity, js.field2
FROM student st
JOIN major mj ON st.studentID=mj.studentID
JOIN jobSearch js ON mj.studentID=js.studentID
WHERE js.activity = 'Other';
I am building a site and i need to retrieve some information. I have this query.
$SQL = "SELECT distretto_108, provinca_113, regioni_116, tipologia_pdv_106,
richiesta_ccnl_107, coop_va_109, nome_pdv_110,
indirizzo_pdv_111, localita_112
FROM civicrm_value_informazioni_su_tute_le_schede_p_22 ";
I need to add this other code:
WHERE civicrm_event.title_en_US='".addslashes($_GET["titles"])."'
but it's not working...
i need to compare let's say the id of another table with the id of the current table... How to do that?
Thanks in advance...
You should learn something about joining tables...
Do not know what the relation is between the two tables (simply said: what column from one table is pointing to what column at other one), but try something similar (modification needed to meet You DB structure) - now lets assume both tables have related column called event_id:
$SQL = "SELECT distretto_108, provinca_113, regioni_116, tipologia_pdv_106,
richiesta_ccnl_107, coop_va_109, nome_pdv_110,
indirizzo_pdv_111, localita_112
FROM civicrm_value_informazioni_su_tute_le_schede_p_22 cvistlsp22
LEFT JOIN civicrm_event ce ON ce.event_id = cvistlsp22.event_id
WHERE ce.title_en_US='".mysql_real_escape_string($_GET["titles"])."'";
civicrm_value_informazioni_su_tute_le_schede_p_22 table name is very long and You will not be able to create a table with such long name in other DBMS (e.g. ORACLE), so try to make it shorter while still self-describing...
If You want to join tables they have to have a relation, read more about relations and how to use them here: http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/
You are retrieving the data from table civicrm_value_informazioni_su_tute_le_schede_p_22 in your query while the where clause you are adding, refers to the table civicrm_event. You need to add this new table in the from clause and do a join among the two tables using some common key. Example below:
$SQL = "
SELECT distretto_108, provinca_113, regioni_116, tipologia_pdv_106, richiesta_ccnl_107, coop_va_109, nome_pdv_110, indirizzo_pdv_111, localita_112
FROM civicrm_value_informazioni_su_tute_le_schede_p_22
JOIN civicrm_event ON civicrm_value_informazioni_su_tute_le_schede_p_22.ID_PK = civicrm_event.ID_FK
WHERE civicrm_event.title_en_US='".addslashes($_GET["titles"])
";
You need to replace the ID_PK and ID_FK with the relevant Primary and Foreign Keys that bind the tables together.
Please note using query params like that is not recommended. Please read PHP Documentation here for more explanation.
I am building a query involving a JOIN. This is the first time I've done db stuff with Active Record and I've hit a bit of a snag.
I want to join a table called companies to the users table so I can get the name of the company etc the user is in. I've done this sort of successfully like so:
function get_profile_by_username($username)
{
$this->db->join('companies', $this->table_name.'.company_id = companies.id');
$this->db->where('LOWER(username)=', strtolower($username));
$query = $this->db->get($this->table_name);
if ($query->num_rows() == 1) return $query->row();
return NULL;
}
However the issue being that the fields in companies, they are id and name are returned in that object as simply called name.
Normally when I would write the raw query I would give aliases to the tables and the result would be something like u.company_id, c.name. So I'd know name had nothing to do with the user but of course is the name of the company. And although not an issue now but potentially in the future, the id column obviously can't coexist in the result set, so one gets overwritten!
How can we get this sort of differentiating between the fields that come from certain tables? Or is there a better way of going about table joins and working with joined query data sets/objects?
Edit:
If I was doing it as a raw query I'd do:
SELECT u.id, u.username, c.name
FROM users AS u
JOIN companies AS c
ON c.id = u.company_id
WHERE u.username = 'foobar';
Which is great but if I tried to do that in active record I reckon that's pretty poor practice, if it works at all.
If you want to select some specific columns from table use db->select(). You can give alias to tables, add some conditions and etc. Send second parameter FALSE to not escape special characters.
$this->db->select('u.id, u.username, c.name', false);
$this->db->from('user as u');
$this->db->join('companies as c', 'u.company_id = c.id');
$this->db->where('LOWER(u.username)=', strtolower('foobar'));
$query = $this->db->get();
$this->db->select('ut.nombre as nombreu, ut.apellido, ru.nombre as nombrer');
$this->db->from('User_table ut');
$this->db->join('Role_usuario ru', 'ut.role_user = ru.Id');
$query = $this->db->get();`
`