Query: Where hidden = '0' - mysql

How can I make my search form to only list results where hidden = '0'; + rest of the options ?
$stmt = $dbh->prepare("
SELECT *
FROM users u
LEFT JOIN menues m
ON u.user_id = m.restaurant_id
WHERE (restaurant LIKE CONCAT('%', :term, '%'))
OR city LIKE CONCAT('%', :term, '%')
");
$stmt->bindParam(":term", $term);
$stmt->execute();
Thanks in advance :)

Well, I hope I understand what you need:
$stmt = $dbh->prepare("
SELECT *
FROM users u
LEFT JOIN menues m
ON u.user_id = m.restaurant_id
WHERE (((restaurant LIKE CONCAT('%', :term, '%'))
OR city LIKE CONCAT('%', :term, '%'))
AND hidden = 0)
");
$stmt->bindParam(":term", $term);
$stmt->execute();

Can you not just add
AND hidden = '0'
to the end of your WHERE clause so your query ends
.
.
.
WHERE (restaurant LIKE ...
OR city LIKE ...)
AND hidden='0'
Incidentally is hidden really a character-based column? If not, you should be adding hidden=0 (without single quotes)

Related

Using WHERE clause with LIKE and OR

I have this sql statement for my search function.
$a_search = "$a%";
$sql = "SELECT a.*, u.*
FROM articles AS a
LEFT JOIN users AS u
ON a.written_by = u.username
WHERE a.written_by OR a.title LIKE ? // want to search for both
ORDER BY a.written_by ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$a_search);
$stmt->execute();
$result = $stmt->get_result();
// more code ...
Works fine except one thing. It searches the articles ONLY where the a.title match. But I also want to search by the author and that's why I added a.written_by. However it still only searches for the a.title. So my question is how can I search for both at the same time?
What you wrote has no condition on a.written_by
you have to do something like
$a_search = "$a%";
$sql = "SELECT a.*, u.*
FROM articles AS a
LEFT JOIN users AS u
ON a.written_by = u.username
WHERE a.written_by LIKE ? OR a.title LIKE ? // want to search for both
ORDER BY a.written_by ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param($a_search,$a_search);
$stmt->execute();
$result = $stmt->get_result();
// more code ...
This will send the result where a.written_by LIKE $a_search or a.title LIKE $a_search
you cannot give a common or and....gotta mention it in each case...for e.g.
$a_search = "$a%";
$sql = "SELECT a.*, u.*
FROM articles AS a
LEFT JOIN users AS u
ON a.written_by = u.username
WHERE a.written_by ='cat' OR a.title LIKE 'dog' ? // want to search
ORDER BY a.written_by ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$a_search);
$stmt->execute();
$result = $stmt->get_result();
// more code ..

Long tail where clause at CodeIgniter or Ignited Datatables

I am trying to write a long tail where statement which containing another select query and how I can do it. Currently, I am using Ignited Datatables Library with my CodeIgniter 3 installation.
SELECT ContactId,
ReceiverId,
SenderId,
IF(ReceiverId = 1,
(SELECT first_name
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT first_name
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS FirstName,
IF(ReceiverId = 1,
(SELECT email
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT email
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS SenderEmail,
IF(ReceiverId = 1,
(SELECT phone
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT phone
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS SenderPhone,
IF(ReceiverId = 1,
(SELECT company
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT company
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS SenderCompany,
ModulesId,
(SELECT ModuleName
FROM modules
WHERE ModuleId = contacts_connectivity.ModulesId) AS ModuledName,
FROM_UNIXTIME(AddedDate, "%m/%d/%Y") AddedDate
FROM contacts_connectivity
WHERE (ReceiverId = 1
OR SenderId = 1)
AND (ReceiverId
OR SenderId NOT IN
(SELECT (ReceiverId
OR SenderId)
FROM contacts_connectivity
WHERE (ReceiverId
OR SenderId) = 1))
I noticed that My query doesn't return any array at all. I already tried to write the where statement like this-
$this->datatables->where("(ReceiverId = ' . $id . ' OR SenderId = ' . $id . ')");
$this->datatables->where(" AND (ReceiverId OR SenderId NOT IN (SELECT (ReceiverId OR SenderId) FROM contacts_connectivity WHERE (ReceiverId OR SenderId) = ' . $id . '");
What am I doing wrong?
You invalid use parameters. Try something like this:
$this->datatables->where("(ReceiverId=$id OR SenderId=$id )");
Second string should be fixed too the same way. Also check that your $id param properly escaped.

query execution is too long to load

can i minimize or shorter (if ever) this query? this query takes too long to load, how can I shorten the execution of this query? thanks.
this is my sql query:
$sql = "
SELECT
i.ID
FROM item_tb i
WHERE
i.coID = '". $_SESSION['coID'] ."'
AND i.isProduct = '1'
AND i.isBom = '0'
AND NOT EXISTS(
SELECT
s.ID
FROM
stocks_tb s
WHERE
i.ID = s.itemID
AND s.brID = '". $brID ."'
)
";
I guess that your query converted LEFT JOIN as follows:
SELECT
i.ID
FROM item_tb i LEFT JOIN stocks_tb s ON i.ID = s.itemID
WHERE
i.coID = '". $_SESSION['coID'] ."'
AND i.isProduct = '1'
AND i.isBom = '0'
AND s.brID = '".$brId."'
AND s.itemID IS NULL;
and this is faster than your query.
You use many AND operator... Use bracket within the AND condition.. That's it

Issue in left join using mysql query

I have this query here
$rs = mysql_query("select Username.users,phone.users,user_id.coupon,points.coupon,time.coupon from users LEFT JOIN coupon on Username.users = coupon.user_id where coupon.user_id like '%$search%' or coupon.time like '%$search%' order by $sort $order limit $offset,$rows");
Where
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'time';
Am i doing any error here?
I don't get any result,
please help me with this.
Thanks..
You are selecting columnname.tablename instead of tablename.columnname. So instead of Username.users use users.Username and all respective.
select users.Username --<-- here
,users.phone ,coupon.user_id. ,coupon.points ,coupon.time
from users LEFT JOIN coupon
on users.Username = coupon.user_id --<-- also here
where ...
Also, you can use alias to reduce your query length and time of writing query and for more simplicity like this:
select u.Username --<-- here
,u.phone ,c.user_id. ,c.points ,c.time
from users u LEFT JOIN coupon c
on u.Username = c.user_id --<-- also here
where ...
Try to write the query the following way which may help:
$rs = mysql_query("select users.Username,users.phone,coupon.user_id,coupon.points,coupon.time from users LEFT JOIN coupon on users.Username = coupon.user_id
where coupon.user_id like '%$search%' or coupon.time like '%$search%' order by $sort $order limit $offset,$rows");
Just verify that users.Username is in fact the correct id to join on the coupon table. Shouldn't it be users.user_id? In which case it will be
$rs = mysql_query("select users.Username,users.phone,coupon.user_id,coupon.points,coupon.time from users LEFT JOIN coupon on users.user_id = coupon.user_id
where coupon.user_id like '%$search%' or coupon.time like '%$search%' order by $sort $order limit $offset,$rows");
more appropriate method,
as time is a type in Sql
select users.`Username`, users.`phone`, coupon.`user_id`, coupon.`points`,coupon.`time` from users
LEFT JOIN coupon
on users.`Username` = coupon.`user_id`
where coupon.`user_id` like '%$search%' or coupon.`time` like '%$search%'

How to write a complex sql query in zend2

I want to write a mySQL query like the following in zend framework 2.
How to convert it in zend Db select convention?
SELECT profiles.*,
user.email,
user.first_name,
user.last_name,
portfilio_images.profileImage,
images.url AS imgurl
FROM profiles
INNER JOIN user ON user.user_id = profiles.ownerId
LEFT JOIN (
SELECT *
FROM portfilio_images
WHERE portfilio_images.profileImage = '1'
) as portfilio_images ON portfilio_images.profileId = profiles.id
LEFT JOIN images ON images.id = portfilio_images.imageId
WHERE profiles.ownerId != '4' AND (profiles.name LIKE '%a%' OR user.first_name LIKE '%a%' OR user.last_name LIKE '%a%')
GROUP BY user.user_id
You can also use Zend\Db\Sql\Select that allows you to build complex queries programatically. See the documentation here:
http://zf2.readthedocs.org/en/release-2.1.1/modules/zend.db.sql.html#zend-db-sql-select
You could always just perform a raw query:
$sql = "SELECT * FROM test";
$statement = $abAdapter->query($sql);
$result = $statement->execute();
$resultSet = new ResultSet;
$resultSet->initialize($result);
return $resultSet;
Or if you want to add some parameters
$sql = "SELECT * FROM test where col = ?";
$statement = $dbAdapter->query($sql);
$result = $statement->execute(array(99));
$resultSet = new ResultSet;
$resultSet->initialize($result);
return $resultSet;