I am using this code :
<?
$i_count=1;
$sSQL = "SELECT * FROM projects WHERE status=1 AND location LIKE '%" . $a_project[0]["location"] . "%' ORDER BY delivery_year DESC, project_id DESC LIMIT 0,5";
$mysql_result = mysql_query($sSQL, $GLOBALS['conn']);
$num_rows = mysql_num_rows($mysql_result);
if ($num_rows > 0) {
?>
<br />
It will show the project with same location with viewing project. But it also show the current project in the result.
Ex : I have 02 projects in location Hanoi name A and B. When I view project A, the related projects show both A and B project.
How I can fix it ?
Thanks
Exclude the current project in the WHERE:
SELECT * FROM projects WHERE status=1 AND location LIKE '%" . $a_project[0]["location"] . "%' AND id <> " . $a_project[0]["id"] . " ORDER BY delivery_year DESC, project_id DESC LIMIT 0,5
(Or something like it, depending on your field names and such)
Related
so I have this query below in my php code :
$query ="SELECT *
FROM material_tools_master_data
WHERE material_name like '" . $_POST["keyword"] . "%'
ORDER BY material_code
LIMIT 0,50";
It does pretty well and give me a result called 'autocomplete' in my form. The problem is, I wanna make it more complex, I want my autocomplete filter the data selection not only by material_name but also with material_tools_group and show me exactly the material_name which is filtered by material_group = 'Measuring' OR 'Tools'.
The point is, I want to make this query works with my autocomplete. So here is my new query :
$query ="SELECT *
FROM material_tools_master_data
WHERE `material_tools_group` = 'Measuring' OR 'Tools' AND `material_name` like '" . $_POST["keyword"] . "%'
ORDER BY material_code LIMIT 0,50";
The query above is not working, the query above is giving me all the material_name rows in the table.
Any help will be much appreciated.
See warnings about PHP's deprecated API, and the proper use of prepared statements above...
$query ="
SELECT *
FROM material_tools_master_data
WHERE material_tools_group IN('Measuring','Tools')
AND `material_name` LIKE '" . $_POST["keyword"] . "%'
ORDER
BY material_code LIMIT 0,50;
";
I'm trying to put together 2 sql query one for name :
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."'ORDER BY id DESC";
and one to get $from and $to:
$sql = "SELECT * FROM register WHERE time BETWEEN ('$from') AND ('$to')";
Can you suggest how it is possible to combine these two query to show one name selected aswell and from and to for that name that is selected aswell as to just show all results for name selected without the from and to?
SELECT * FROM register WHERE time BETWEEN #from AND #to
UNION
SELECT * FROM register WHERE name LIKE #name ORDER BY id DESC
This can be done using UNION, or alternatively, you could use OR condition:
SELECT *
FROM register
WHERE name LIKE '" .$name ."'
OR time BETWEEN ('$from') AND ('$to')
ORDER BY id DESC;
SELECT * FROM register WHERE name LIKE '" .$name ."'
OR time BETWEEN ('$from') AND ('$to')
ORDER BY id DESC
You can use Union to combine the results of both queries
SELECT * FROM register WHERE name LIKE '" .$name ."'
union all
SELECT * FROM register WHERE time BETWEEN ('$from') AND ('$to')
Your question is unclear but still can try something like this
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."'
OR (time BETWEEN ('$from') AND ('$to')) ORDER BY id DESC";
This is what I was after, thanks to my Oracle programmer you know who you are, Thought I share:
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."' and time between ifnull(timestamp('$from'),time) and ifnull(timestamp('$to'),time) ORDER BY id DESC";
Hi I am trying to retrieve my topics from table topics but in my look up list I want the most recent active topic to be showing, all the comments are in different table. How could I do it that my list shows all the topics from the topics table but puts them in order according to the most recent date from the comments table?
$forums = mysql_query("select * from forumtopic where category='$who' order by date DESC LIMIT " . (($page - 1) * 10) . ", 10");
while($forum = mysql_fetch_array($forums))
{
so I want it to be like
$forums = mysql_query("select * from forumtopic where category='$who' and select from forumcomment where category='$who' but order by date from forumcomment DESC LIMIT " . (($page - 1) * 10) . ", 10");
while($forum = mysql_fetch_array($forums))
{
if that makes sense
I believe this is what you wanted to do:
SELECT * FROM forumtopic
WHERE category='$who'
ORDER BY FIELD( topic,
(SELECT topic
FROM forumcomment
WHERE category='$who'
ORDER BY date DESC))
LIMIT " . (($page - 1) * 10) . ", 10
The order by field lets you specify an order for the table forumtopic, which is ordered by most active, also I don't know the schema of your table so you have to change the names accordingly
EDIT - SIMPLER SOLUTION, JOIN THEN ORDER BY
SELECT forumtopic.topic FROM forumtopic
INNER JOIN forumcomment
ON forumtopic.topic = forumcomment.topic
WHERE category = '$who'
ORDER BY forumcomment.date DESC
LIMIT " . (($page - 1) * 10) . ", 10
I have 2 tables
members
=======
id
f_name // get these values.
l_name
email
friends
=======
to
from
The users's I'd is the value $member_id, if it is present in "to" or "from" I want it to not return the other value of that row, so only non-friends are shown.
Im creating a page to allow members to search through the database to add their friends by email, or name.
I'd like to return all rows from members where there is no record of the userid in either the "to" or "from" columns of the friends table.
I know how to do most basic mysql but joins are an issue for me, i don't really understand how to write them, if anyone can help me out and maybe even explain it a bit that would be great!
My query now:
$query = "SELECT * FROM members WHERE f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10";
//Where $word is the search term.
Modified Query
SELECT * FROM members WHERE id NOT IN (SELECT to FROM friends WHERE frm=$member_id) AND $member_id NOT IN (SELECT frm FROM friends WHERE to=$member_id) AND f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10
Edited
So, suppose a user with id 3:
SELECT *
FROM members
WHERE id <> 3
AND id NOT IN (SELECT to FROM friends WHERE from = 3)
AND id NOT IN (SELECT from FROM friends WHERE to = 3)
[Other conditions....];
OR
SELECT *
FROM members
LEFT JOIN friends
ON (members.id = friends.to
OR members.id = friends.from)
AND (members.to = 3
OR members.from = 3)
WHERE friends.to IS NULL
AND id <> 3
[Other conditions....];
Maybe consider change the name of your "from" and "to" columns, to avoid confusion, because FROM and TO are reserved words.
Also, you can use the reserved word EXPLAIN , before the query, to see the difference of the number of rows being fetched.
You might be able to do that using an LEFT Join, which will return all rows from members even if there's no corresponding row on the friend's table.
Something like this:
SELECT a.*, f.* FROM members m LEFT JOIN friends f
A great article to understand the many joins there are is this one.
I am building a job search site where candidates can register themselves for various jobs . Also employers can post jobs with multiple qualification (eg if he wants MBA or Phd it will be saved in database like mba,phd . I want to show jobs for the registered candidates which are relevant to his educational qualification . So I am confused how can I do this because I think I have to search within database . Please help.
if($cid >0 ) //If the candidate is registered
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE "; //I want to put a condition if he is registered with us , it will show jobs a/c to his qualification
}
UPDATE :
$cond = "";
if($cid >0 )
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE emp_qualification LIKE '%$quali%'";
}
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
$cond
ORDER BY job_add_date DESC LIMIT 0,10
";
// Query Search all jobs
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
ORDER BY job_add_date DESC LIMIT 0,10
";
You use the LIKE keyword - for example,
SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
WHERE emp_qualification LIKE '%MBA%'
ORDER BY job_add_date DESC LIMIT 0,10
Here, the '%' wildcard is used for any number of characters before or after MBA, so it will find it in strings like MBA, BTech, MBA, BCom, MBA, PhD.
set a colum in your tbl_emp_data names 'is_registered' type ENUM and set two value ('0','1')
and then set WHERE condition
WHERE is_registered = 1