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.
Related
I have to search the database for certain results which are in one table but are not in a certain column in another table. my query below is what I have so far.
$sql = "SELECT * FROM users WHERE name LIKE '%".$_POST["search"]."%' AND NOT IN
(SELECT friend FROM friends WHERE user='{$user_name}')";
as you can see I have the WHERE and then I declare the first item it has to search for. But then I add in AND so I am can search another table which is in the brackets.
What is the best way I can do this action without having to change my format to much?
if anyone has any questions please post them below. So I can improve my question quality in the future please post below any ways that I can improve.
The problem isn't the use of two WHERE clauses, it's that instead of writing:
... WHERE name LIKE '%xxx%' AND NOT IN (...) ...
you have to write:
... WHERE name LIKE '%xxx%' AND name NOT IN (...) ...
"SELECT * FROM users left join friends on users.name=friends.friend WHERE friends.friend IS NULL AND users.name LIKE '%".$_POST["search"]."%'"
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 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();`
`
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');
I want to make a search engine in an intranet. Now i use this clause in PHP.
$k = explode(" ",$_GET[key]);
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like '%$k[0]%' or '%$k[1]%'";
But it seems doesn't work. Do you know where is wrong?
Thanks in advance.
Edit:
$sql = "select * from entreprise, site, salarie where entreprise.*, site.*, salarie.* like '%$k[0]%' or '%$k[1]%'";
I have modified the query clause. With this code, i think you can know what i want to do.
I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table.
It's hard to exactly see what you're trying to do, but you need, in your SQL query, to specify :
on which tables you are working, with a from clause
on which fields the search has to be done, in the where clause.
how the data between employees and enterprises are related :
do you want to search for entreprises and their employees ?
for employees and there enterprises ?
for all enterprises and the employees when the employee or the enterprise contains the words ?
You could use something like this to search for entreprises that contain the word, and get their employees to :
select *
from entreprise
inner join employee on employee.id_entreprise = entreprise.id
where entreprise.name like '%word%'
or entreprise.description like '%word%';
Or, to search for employees that match the criteria and get their entreprise too :
select *
from employee
inner join entreprise on entreprise.id = employee.id_entreprise
where employee.name like '%word%';
(just some ideas -- you'll have to build from there !)
This:
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like '%$k[0]%' or '%$k[1]%'";
is not valid SQL. It is hard to guess what you want to do, but I'm trying anyway: you want to find employees, and search them by name or by enterprise that employs them. Is that the case? Or do you want to search employess and/or enterprises?
EDIT
I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table.
Ok, first of all you should realize that SQL is probably not the best tool for this job. See the other commenter - his suggestions about sphinx and friends are good. But still, if you really want to:
$sql = '
SELECT e.id, e.name
FROM enterprise e
-- first, look in column1
WHERE e.column1 LIKE '."'%".$k[0]."%'".'
OR e.column1 LIKE '."'%".$k[1]."%'".'
...etc for all entries in k...
OR e.column1 LIKE '."'%".$k[N]."%'".'
-- then, look in column2
OR e.column2 LIKE '."'%".$k[0]."%'".'
OR e.column2 LIKE '."'%".$k[1]."%'".'
...and so on and so forth for all entries in $k and all columns in enterprise...
UNION ALL
SELECT s.id, s.name
FROM salarie s
WHERE ...and the same for columns of salarie...
...
UNION ALL
...any other tables you want to search...
';
As you can see, not something that makes you happy.
Another approach that might give you more joy is having some overnight job to scan all rows in the tables you're interested in, parse the texts you want to search into separate words, and store those in a keyword table, and storing the association between an object from the source database and the keyword in a separate table. You can then search the keyword table and use the id's and table names you find for a collection of keywords to build the actual query to retrieve those rows. This is what I do, and it works great. It works better because there is a relatively small amount of words that you will encounter, whereas the collection of objects is quite possible very large.