PHP/MYSQL: Using 'CONCAT' and 'AND' clause in MYSQL Query - php

I'm trying to execute this query:
Query 1:
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
ORDER BY a.`wo_number` DESC LIMIT 0,50";
Query 2:
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
AND a.`status` = 'NULL'
ORDER BY a.`wo_number` DESC
LIMIT 0,50";
The Query 2 didn't gave me any result with AND clause while the Query 1 gave me the result.
Can anyone help me with this? I need to sort out the result which has the empty status in my table, that's why I added AND clause in Query 2 hoping the result will be as expected, but it's not.
Thank You.

Unless NULL is an actual string, you need to use IS NULL instead.
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
AND a.`status` IS NULL
ORDER BY a.`wo_number` DESC
LIMIT 0,50";

Related

CodeIgniter get table count rows

I am new in CodeIgniter, I want to count all rows from database table but i use limit in query and i want all count without use limit how can i do ?
my code is below :
$sql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC
LIMIT $start, $per_page ";
$query = $this->db->query($sql)
i use limit for pagination but i want to get all record from table.
You can add new column in both above and below UNION queries. It will be like below.
select (select count(*) from your_query), your_columns from query_above_union
UNION
select (select count(*) from your_query), your_columns from query_below_union
your_query = your full actual query your are using currently.
Although I am not sure about Codeigniter. But sure about SQl.
* If you count all records with all data including limit, than you can use this code. please check it. I hope it will works for you.*
$countsql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC";
$sql = $countsql. " LIMIT $start, $per_page";
$totalRecords = $this->db->query($countsql);
$result["total_rows"] = $totalRecords->num_rows();
$query = $this->db->query($sql);
$result["list"] = $query->result_array();

mysql return result from table related to other tables

I'm trying to show result from table that related to many tables but my problem is that the query is return only one product that is on special what I want to return are both products products on special and also products that are not.
My Php Query is :
$query = "SELECT DISTINCT p.product_id, p.price ,sp.date_end, f.percentage AS special_percentage , p.model, pd.name AS title, pd.description AS text, cd.name AS section, p.image, pd.tag, p.date_added AS created "
."FROM #__mijoshop_product AS p "
."JOIN #__mijoshop_product_special AS sp ON p.product_id = sp.product_id "
."JOIN #__mijoshop_flordeco_product_special_percentage AS f ON sp.product_id = f.product_id "
."INNER JOIN #__mijoshop_product_description AS pd ON p.product_id = pd.product_id "
."LEFT JOIN #__mijoshop_product_to_store AS ps ON p.product_id = ps.product_id "
."LEFT JOIN #__mijoshop_product_to_category AS pc ON p.product_id = pc.product_id "
."LEFT JOIN #__mijoshop_category_description AS cd ON (pc.category_id = cd.category_id AND cd.language_id = {$language_id}) "
."LEFT JOIN #__mijoshop_category_to_store AS cs ON (pc.category_id = cs.category_id AND cs.store_id = {$store_id}) "
."WHERE (LOWER(pd.name) LIKE '%" . $search_text . "%' OR
LOWER(pd.description) LIKE '%" . $search_text . "%' OR
LOWER(p.sku) LIKE '%" . $search_text . "%' OR ";
if( $model ) {
$query .= "LOWER(p.model) LIKE '%" . $search_text . "%' OR ";
}
$query .= "LOWER(pd.tag) LIKE '%" . $search_text . "%') "
."AND p.status = '1' "
."AND date(sp.date_end) >= date(NOW()) "
."AND p.date_available <= NOW() "
."AND ps.store_id = {$store_id} "
."AND pd.language_id = '" . $language_id . "' "
."GROUP BY p.product_id "
."ORDER BY {$order_by} "
."LIMIT ".$limit;
$db->setQuery($query);
$results = $db->loadObjectList();
Change the joins with #__mijoshop_product_special and #__mijoshop_flordeco_product_special_percentage to LEFT JOIN so that it won't restrict the results only to products that have matches in this table.
Also, you don't need to use SELECT DISTINCT when you use GROUP BY p.product_id; since there's only 1 row for each product ID, you can't get any duplicates. However, it also doesn't make sense to use GROUP BY when you're not using any aggregation functions, like SUM() or COUNT(). If all these tables are 1-to-1 correspondences, you shouldn't get any duplicates that need to be removed with either option.

SQL Join Issue PHP

I'm trying to do a SQL join to return records from one table for users who don't have a corresponding entry in a second table against the record in the first table. The query I'm trying is below, which I think should return what I want, but it's giving a syntax error:
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE '" . $_SESSION['userEmail'] . "' NOT IN matches.user";
Any ideas or suggestions? What I'm trying to achieve from the above is to only show results for which the logged in user hasn't marked the supplier record as viewed.
TIA
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user NOT IN '" . $_SESSION['userEmail'] . "'";
not in clause not added properly.
correct your where clause :
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user NOT IN ('" . $_SESSION['userEmail'] . "') ";
OR if it is single value then you can use != instead of not in
$query = "SELECT * FROM `suppliers` LEFT OUTER JOIN `matches` ON suppliers.supplierid = matches.supplier_id WHERE matches.user != '" . $_SESSION['userEmail'] . "' ";
You should swap your WHERE Conditions and trim with brackets ().
$query = "
SELECT * FROM `suppliers` LEFT OUTER JOIN `matches`
ON suppliers.supplierid = matches.supplier_id
WHERE matches.user NOT IN ('" . $_SESSION['userEmail'] . "')
";
Your logic is just not correct. You can use LEFT JOIN, but the query should look like:
SELECT s.*
FROM suppliers s LEFT OUTER JOIN
matches m
ON m.supplierid = s.supplier_id AND
m.user IN ('" . $_SESSION['userEmail'] . "')
WHERE m.supplierid IS NULL;
Note: This row may be causing a syntax error or unexpected results:
m.user IN ('" . $_SESSION['userEmail'] . "')
You will need to look at the syntax after variable substitution to figure that one out. This answer addresses the logic of the query, so it will do what you actually want when the syntax is fixed.

mysql select something as where is not null

I'm quite new at this, so I have question about MySQL. I have query in openCart:
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name
ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id,
c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') ."'";
In MySQL I added new column "show_menu" (it can be NULL or 1) and now I want to change my query, that It will return only categories in where show_menu = 1
As I understand I need something like: WHERE show_menu IS 1.
Maybe anyone can help where to put it? because I tried, but no luck...
Use show_menu = 1 or show_menu IS NOT NULL
You just can't use the '=' operator with a null value.
Just add the following to your WHERE clause:
... AND show_menu = 1
Just Like
SELECT * FROM tablename
WHERE conditions_from_your_existing_query
AND show_menu = 1
You can use show_menu = 1 or you can use show_menu is not null. Either way will work.

Select query returning 1 result instead of 3 because of AVG

The select query below returns 1 row when it should be 3. I am pretty sure it is because of the AVG(k.sumtotal) field.
If I rewrite the query and take out that AVG(k.sumtotal) column and take out the FROM inv_ratings AS k, I get my 3 rows.
I can't figure out why this table (inv_ratings) or this AVG(k.sumtotal) column restrict the number of rows to 1. I looked online for hours trying to find information about returning results using the AVG clause and didn't find much. Do I have to use a group by clause, I tried that and only get errors.
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
$q = mysqli_query($dbc, $p) or trigger_error("Query: $p\n<br />mysqli Error: " . mysqli_error($dbc));
You are running into one of MySQL's gotchas:
http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
I hate that MySQL ever allows this syntax because it only causes confusion. But what you probably want is (to use MySQL's hackish behavior, please note that if there are multiple values for any of the fields besides invention_id or sumtotal, you get a random value from that column):
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
Or, to not use MySQL's hackish behavior:
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";

Categories