I'm using get request to fetch serial and put it into query.
$id = $_GET['id'];
$select = $pdo->prepare("SELECT * FROM tbl_product WHERE serial='.$id.'");
$select->execute();
while($row = $select->fetch(PDO::FETCH_OBJ)){
echo '
<tr>
<td>'.$row->pname.'</td>
<td>'.$row->pcategory.'</td>
<td>'.$row->serial.'</td>
<td>'.$row->sku.'</td>
';
}
My table look like this, I tried using inner join for same table but it didn't work.
When user key in serial through get request localhost?id=12345678 . I want it to show item1 - item3, that match with its SKU
id | sku | serial | product_name
0 | ABC | 12345678 | item1
1 | ABC | 87654321 | item2
2 | ABC | 23456789 | item3
3 | DEF | 56789234 | item4
Something like
select * from tbl_product where sku = (select sku from tbl_product where serial = :serial)
might do it, presuming a PDO prepared statement.
You can try inner join like this:
select id, c.sku, serial, product_name from table_name t
inner join (
select sku from table_name where serial ='12345678'
)c on t.sku = c.sku
in your code:
$id = $_GET['id'];
$query = "select id, c.sku, serial, product_name from table_name t
inner join (
select sku from table_name where serial = :serial
)c on t.sku = c.sku";
$select = $pdo->prepare($query);
$select->bindParam(':serial', $id);
$select->execute();
while($row = $select->fetch(PDO::FETCH_OBJ)){
echo '
<tr>
<td>'.$row->pname.'</td>
<td>'.$row->pcategory.'</td>
<td>'.$row->serial.'</td>
<td>'.$row->sku.'</td>
';
}
Don't trust any input, use prepare, and bindParam to avoid SQL injection. You can reference SQL injection here https://www.php.net/manual/en/security.database.sql-injection.php
Self join
$select = $pdo->prepare("SELECT * FROM tbl_product P1 LEFT JOIN tbl_product P2 ON P2.sku = P1.sku WHERE P1.serial='.$id.'")
Related
I have here two tables, I need to update both table row field from a post value. table1.item and table2.item update from post value. I didn't know how to set both table field equal to post value.
Any help will appreciate.
Table1
| pr_id | item |
----------------
| 1001 | pen |
Table2
| pr_id | item |
----------------
| 1001 | pen |
Query
<?php
$pr = $mysqli->real_escape_string($_POST["pr_id"]);
$item= $mysqli->real_escape_string($_POST["item"]);
$mysqli->query("UPDATE table1 LEFT JOIN table2 ON table1.pr_id = table2.pr_id WHERE table1 .pr_id = '$pr' ");
?>
Try this:
update table1 inner join table2
on table1.pr_id=table2.pr_id
//your changes
set table1.item ='some thing', table2.item ='some thing'
where table2 .pr_id ='$pr' ;
Try this:
Query:
UPDATE table1 T1 LEFT JOIN
table2 T2 ON T1.pr_id = T2.pr_id
SET T1.Item= '$item', T2.Item = '$item'
WHERE T1.pr_id = '$pr'
With php:
$mysqli->query("UPDATE table1 T1 LEFT JOIN table2 T2 ON T1.pr_id = T2.pr_id SET T1.Item= '$item', T2.Item = '$item' WHERE T1.pr_id = '$pr' ");
According to my opinion it is better to use Compound Trigger for updating 2 diffenent tables at the same time. Try to use that.
i try to list out all products with product images, but not sure how to use subquery in this situation. Below is my database structure
products
products_id + product_name +
1 | Apple |
2 | Banana |
products_screenshot
products_id + images +
1 | Apple-1.jpg |
1 | Apple-2.jpg |
1 | Apple-3.jpg |
2 | Banana-1.jpg |
2 | Banana-2.jpg |
2 | Banana-3.jpg |
this is my query:
$sql = "SELECT p.* FROM `products` p ORDER BY p.products_id ASC ";
//LEFT JOIN (SELECT ps.* FROM `products_screenshot` ps WHERE ps.products_id=p.products_id) AS pss ON(p.products_id=pss.products_id)
$query = $db->query($sql);
while ($row = $query->fetch_object()) {
// result
echo $row->products_name.'</br>';
// list all product screenshot related with this
//$row->images
}
With your requirement you can do as below. Then execute the query and loop through.
The
$row->images will be comma seperated and you can split them display them for a product.
$sql = "SELECT p.products_id,p.product_name,group_concat(pi.images) as `images`
FROM `products` p
left join products_screenshot pi on pi.products_id = p.products_id
group by p.products_id
ORDER BY p.products_id ";
If you dont care about products being repeated multiple times in the loop then just use below where for each loop you will get a new row with the product and the related images
$sql = "SELECT p.products_id,p.product_name,pi.images
FROM `products` p
left join products_screenshot pi on pi.products_id = p.products_id
ORDER BY p.products_id ";
What wrong with a simple query like this:
select
p.products_id,
p.product_name,
ps.images
from
products p
join products_screenshot ps
on p.products_id=ps.products_id
where
whateveryouline
order by
1,2
It will get you all the items you need?
Try this query:
select products.product_name, products_screenshot.images from products,products_screenshot where products.products_id = products_screenshot.products_id
SELECT * from products_screenshot as ps where products_id IN (SELECT p.products_id from products as p WHERE p.products_id = ps.products_id )
Hi as the title say i have a php code where i sucefully echo the articles in a table, the problem is in this table i just have cat_id the parent_id is referenced in other table..
"articulos" table
id | titulo | fecha_evento | descripcion | img | cat_id
"categoriablog" table
id | parent_id
This is the way im making my query
$query1 = "SELECT id,titulo,fecha_evento,descripcion,img FROM articulos WHERE cat_id = 1";
$result = mysql_query($query1);
My goal is to do something like this, but "parent_id" is in other table
$query1 = "SELECT id,titulo,fecha_evento,descripcion,img FROM articulos WHERE cat_id = 1 OR parent_id = 1";
$result = mysql_query($query1);
JOIN the two tables:
SELECT
a.id, a.titulo, a.fecha_evento, a.descripcion, a.img
FROM Article AS a
INNER JOIN Category AS c ON a.cat_id = c.id
WHERE a.cat_id = x
OR c.parent_id = x
You can use a sub query
SELECT id,titulo,fecha_evento,descripcion,img FROM Article
WHERE cat_id = 1 OR cat_id IN
(SELECT id from Category where parent_id=1)
Edit
Please note that this solution is a little bit slower than the JOIN solution in the answer by Mahmoud.
I have two table like
table: t1
id|name|type
1 | a |0
2 | b |1
3 | c |0
4 | d |0
...
table: t2
uid|type
4 |1
3 |0
...
I want to count or get records in table t1 have type = 1 and records have id = uid in table t2 has type = 1
What is the best way to count or get record in one query like
$type = 1;
$count = mysql_num_rows(mysql_query("Select * .... Where type = $type ")); //$count = 2
$result = mysql_query("Select * ... order by id desc "); // $result include record in table t1 has id (4, 2) b/c order by id desc
Edit:
i made a sql to try in http://www.sqlfiddle.com/#!2/8847d1/12
But i can't do that
I want the records result look like (all column have my above conditional)
id|name|
4 |d |
2 |b |
How to do that thank
select t1.id,t1.name from t1 left join t2 on t1.id = t2.uid where t1.type = 1 or t2.type = 1
Since there is no link between the tables you can use a UNION query:
SELECT id,type FROM t1 WHERE type=1
UNION
SELECT uid AS id,type FROM t2 WHERE type=1
http://dev.mysql.com/doc/refman/5.0/en/union.html for more info on union joins.
try this
select * from t1 join t2 on t1.id = t2.uid where t1.type = 1 and t2.type = 1
read how to use join(left join,right join,join)
1. read here
2. 2nd docs
$type = 1;
if ( isset($type) and $type > 0 )
{
$sql = "SELECT id, name FROM t1
LEFT JOIN t2 ON t1.id = t2.uid
WHERE t1.type = $type AND t2.type = $type
ORDER BY t1.id DESC";
$count = mysql_num_rows( $sql ) or die('Error');
$result = mysql_query( $sql ) or die('Error');
}
I have multiple tables in my database. Let's say the table users looks like this:
Users:
|id|name|gender|access|id_ext|
|1 | a | m | 1 | 32 |
|3 | b | m | 3 | 33 |
|4 | c | m | 1 | 34 |
|5 | d | f | 1 | 35 |
I would like to select the user with for example id_ext = 32 and then run another select statement using that selected users fields.
I can solve this by first getting the user with a query and then create another query with users info, but there must be a way to do this in the same query?
This is the query i use now:
SELECT * FROM users NATURAL JOIN
(SELECT id FROM ages WHERE age BETWEEN
(SELECT limit_age_l FROM users WHERE id=17)
AND (SELECT limit_age_h FROM users WHERE id=17)) as a
WHERE NOT id = 17
AND locale = 'en_US'
AND limit_gender = 1
AND visible = 0
AND NOT EXISTS (SELECT view_id FROM matches WHERE user_id = 17 AND view_id = a.id)
LIMIT 1
Problem is that the values id=17, limit_gender=1 and locale = 'en_US' in the query are not known. These are taken from the user with id_ext = '32'.
SELECT * FROM Users WHERE id in (SELECT id FROM Users WHERE id_ext='32');
Yes - assuming your subsequnt query is of the form:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = N /* previously selected id from users */
Then either by using the first query as a subquery:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = (select id from users where id_ext ='32')
/* replace = with IN if more than one id will be returned */
Or by joining to the results of the first query as part of the subsequent query:
select field1, field2, ...
from users
join Table1 on Table1.id = users.id
join Table2 on ...
where ...
and users.id_ext ='32'
(Note that both of these forms assume that users is not already being joined in the existing query - if it is, just add the users.id_ext ='32' condition to the existing query.)
EDIT: If I have understood the requirements correctly, the required query could be written as:
SELECT u.*
FROM users u
join ages a on u.id = a.id and
u.age between limit_age_l and limit_age_h
join users ul on ul.id = 17 and
ul.id <> u.id and
ul.locale = u.locale and
ul.limit_gender = u.limit_gender and
ul.visible = u.visible
AND NOT EXISTS (SELECT NULL
FROM matches m
WHERE m.user_id = ul.user_id AND m.view_id = a.id)
LIMIT 1
SELECT * FROM users WHERE id = (SELECT id FROM users WHERE id_ext = '32');
Select * from users as user inner join userinfo as usinfo on usinfo.id=user.id_ext where user.id_ext='32'