I have a search query that works great, but I need it to only show results where visible = true. That's what needs to be added to the query, I just don't know how to do it correctly. Here's the query.
$query = $mysqli->query("SELECT
id,name,company,town FROM people
WHERE name LIKE '%$mysearchString%' OR
company LIKE '%$mysearchString%' OR
town LIKE '%$mysearchString%' ORDER BY
name DESC LIMIT 100");
The $mysearchString var is just a sanitized $_GET['s'].
SELECT id,name,company,town
FROM people WHERE
(name LIKE '%$mysearchString%'
OR company LIKE '%$mysearchString%'
OR town LIKE '%$mysearchString%')
AND visible = true
ORDER BY name DESC LIMIT 100
$query = $mysqli->query("SELECT id,name,company,town FROM people WHERE (name LIKE '%$mysearchString%' OR company LIKE '%$mysearchString%' OR town LIKE '%$mysearchString%') AND visible = true ORDER BY name DESC LIMIT 100");
Related
I have 2 columns in my database. One is named "name01" and the other is named "name02". This database displays friends on my app. When I go to my friends list on the app I want all my friends displayed by name in asc order. The problem is users names could be in "name01" or "name02" depending on who sent the friend request first. How would you "order by" both name01 & name02 at the same time? I have tried doing it with a comma (ORDER BY name01, name02) but it orders one first and then the other so the names aren't all in alphabetical order.
$sql = mysql_query("SELECT * FROM friends WHERE id01 = '$id' AND status01 = '1' OR id02 = '$id' AND status02 = '1' **ORDER BY name01, name02** Limit 500");
Above will order name01 separately from name02.
Use the GREATEST() function and then ordering:
SELECT friends.*, GREATEST(IFNULL(name01, 0), IFNULL(name02, 0)) as greatest_name
FROM friends
WHERE id01 = '$id' AND status01 = '1' OR id02 = '$id' AND status02 = '1'
ORDER BY greatest_name Limit 500
I want to show data from database if status is enable and package type is domestic and order by id desc. Here is my code:
$sql = "select *
from holidays
where pkg_status = 'Enable'
and pkg_type = 'Domestic'
limit 6";
I want to show this data as order by id desc. When I am trying to add query like this, it is not working.
$sql = "select *
from holidays
where pkg_status = 'Enable'
and pkg_type = 'Domestic'
limit 6
order by id desc";
Please help me.
limit is always written after group clause in SQL
Here you go, just copy and paste, you will be fine :
SELECT * FROM holidays
WHERE pkg_status='Enable'
AND pkg_type = 'Domestic'
ORDER BY id DESC LIMIT 6;
I have written a MySql query to get the columns related with minimum id . Looks something like this
SELECT min(id) as ID,feed , idpropiedad FROM `registrofeed` WHERE feed=21
The table has 4 rows looks like this
So according to the function that I have written
function setLC()
{
$sql = "
SELECT min(id) as ID
, feed
, idpropiedad
FROM `registrofeed`
WHERE feed=21
";
$result = $this->localDb->execute($sql);
$row=mysql_fetch_array($result);
echo $sql;
echo $row['idpropiedad'];
$this->lastCode = $row['idpropiedad'];
}
It returns empty string for idpropiedad
Can any one help me out where I am going wrong
Thanks in advance
I'd think the query you're actually looking for is this:
SELECT id, feed, idpropiedad
FROM registrofeed
WHERE feed = 21
ORDER BY id ASC
LIMIT 1
MIN() is giving you the generally lowest value in the column, it does not affect the rest of the columns. If you want the whole row with the lowest id it doesn't help.
To illustrate, if you really wanted to use MIN here, you'd have to do:
SELECT id, feed, idpropiedad
FROM registrofeed
WHERE id = (SELECT MIN(id) FROM registrofeed WHERE feed = 21)
You can do a better query like this:
$sql = "
SELECT id as ID
, feed
, idpropiedad
FROM `registrofeed`
WHERE feed=21
HAVING MIN(id)
";
This will return only one row with the minimum id number. It's more readable than using ORDERING AND LIMIT 1.
try your select query as
SELECT * FROM registrofeed WHERE feed='21' ORDER BY id ASC LIMIT 1
this fetches the row having minimum id.
Hope it helps
Try this
$sql = "SELECT min(id) as ID,feed , idpropiedad FROM `registrofeed` WHERE feed='21' order by id asc";
I have a query like this :
$pagenumber = get_page_number(); // I'll get the pagenumber by a method wich runs when user click's on page numbers , for example : 2 or 3 or whatevere user clicks.
//Now , I'll make A Limit caluse :
$limit = $pagenumber.",10"; // limit the page to show 10 result => example : LIMIT 2,10
$sql = "SELECT * From doctors LIMIT $limit";
--> after this , I show the result which is by my on way (echo a div for every result );
It was a cool pagination , which was working fine.
But the problem is , when there is A WHERE in my query , mysql gets confused and I dont know what to do
Consider this :
$sql = "SELECT * FROM doctors WHERE `firstname` LIKE '$firstname' LIMIT $limit";
So , its not working:((
I know the problem is "WHERE " clause , because when user clicks on a page number , my $limit will be for example : 2,10
and my query will be this :
$sql = "SELECT * FROM doctors Where firstname LIKE '$firstname' LIMIT 2,10";
Here there is A condition (WHERE firstname ....)
How mysql even knows where to start searching ?(by starting point , I mean if "LIMIT x,y" x is starting point)
How mysql knows what I mean by "LIMIT 2,10".
Where does placed 2 in my table ?(when there is a condition);
If there is NO condition , 2 means record number 21 ( am I right ?), of course if offset is 10 ; (LIMIT 2,10) ;
is that Right?
But , what if there is A WHERE clause , ??????
Now what is that starting point ?(2????)
If you want more specific details , feel free to ask(I think I explained percisely)
Thanks
$pagesize = 10;
$start = ($page) * $pagesize; // assuming you're passing in 0 based pages, if they're 1 base, do a $page -1
$sql = "SELECT * FROM doctors Where firstname LIKE '$firstname' ORDER BY lastname LIMIT $start,$pagesize";
You need to order your result set in order for limit to make sense. Also, start needs to account for page size.
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