I have a problem
not displayed the name for id
data base structure for category_list
id name
1 php
2 mysql
... ...
10 html
and data base structure for entries
category_id
8
5
for the results i have this code, this script displays the ID
as a result it shows 2 , but i want the following to be displayed: mysql
but I want to show name for this ID , instead of ID
We need to use a join query to get the category name for given id, e.g.:
select c.name
from category_list c join entries e on c.id = e.category_id
where category_id = ?
Presumably your database query is something like this:
SELECT category_id FROM some_table
Thus, you're only selecting the ID, not the Name. You can include the Name by joining the other table. Something like this:
SELECT
category_id,
name
FROM
some_table
INNER JOIN category_list
ON some_table.category_id = category_list.id
Then your results would also include the Name value, which you could output to the page:
echo $rows['name']
Run a second query pulling name from category_list like so:
$sql = "SELECT name FROM category_list WHERE id = '$rows['category_id']';";
$result = mysqli_query($conn, $sql);
$category_name = mysqli_fetch_assoc($result);
echo $category_name['name'];
It would however be better to use a JOIN as others like David have pointed out.
Related
Hi everyone this is my first question here and i'll be very grateful if you could help me.
I have a table like this in mysql
//table items
id | item_name | description | link | category_id | is_active
And i have another table like this
//table categories
id category_name | cat_description | is_active
I want to get all the data in category_name and get all the content from item_name if is_active column is on, and also if category is_active column is on.
I was trying to make a function that would retrieve all that data with html content and just if certain conditions are true.
My function is something like this:
function getCatAndItems(){
include "conn.php";
$petition = mysqli_query($conn,"SELECT * FROM items,categories WHERE is_active=1");
while ($row = mysqli_fetch_array($petition)) {
$filename = $row['nombre'];
$url = $row['url_document'];
echo "<a href=../docs/files/$url'><li> ".$filename."</li></a>";
}
}
My goal is to bring the categories and if the categories are active and if the items table in the category_id is the same as the category id and is_active it will bring me also the data in the items table that share the same number.
I hope you understand me and hope you could help me, thank's
You can get using JOIN
SELECT items.name, items.description, items.link, categories.category_name, categories.cat_description
FROM categories
JOIN items
ON categories.id = items.category_id
WHERE categories.is_active = 1
AND items.is_active = 1;
Here is your query :
select i.id,i.item_name,i.description,i.link,i.category_id,i.is_active,c.category_name
from items as i left join categories as c on i.category_id=c.id where
i.is_active=1 and c.is_active=1;
A simple inner join between these two tables would do the job done.
SELECT
items.name,
items.description,
items.link,
categories.category_name,
categories.cat_description
FROM categories
INNER JOIN items ON categories.id = items.category_id
WHERE categories.is_active = 1
AND items.is_active = 1
So I have a table where some of the products repeat with but have a different value on number of clicks.
name ---- clicks
iPhone 4
Samsung 2
iPhone 1
Samsung 5
my select function is :
$select_table2 = 'SELECT * FROM `mytable`'; //WHERE NAME IS THE SAME
$sql2 = $db->query($select_table2);
while ($row = mysqli_fetch_array($sql2)) {
echo $row["name"];
echo $row["clicks"];
}
I need this output:
iPhone 5
Samsung 7
I don't want to merge the same rows because they have one more column that is different. So please do not suggest simply to merge them and update the clicks...
UPDATE:
$pull_request = 'SELECT SUM(e.product_clicks),e.product_id, u.name, u.product_id FROM `oc_aa_affiliatecollclicktracking` AS e GROUP BY e.product_id LEFT JOIN `'.DB_PREFIX.'product_description` AS u ON e.product_id = u.product_id';
I tried it like this but it's not working
use sum() and also GROUP BY name to get desired output.
$select_table2 = 'SELECT name,SUM(clicks) FROM `mytable` GROUP BY name';
$sql2 = $db->query($select_table2);
while ($row = mysqli_fetch_array($sql2)) {
echo $row["name"];
echo $row["clicks"];
}
while will produce,
iPhone 5
Samsung 7
For more info
SUM()
GROUP BY
EDIT
Group by should come after join.
$pull_request = 'SELECT SUM(e.product_clicks) as clicks,e.product_id, u.name, u.product_id FROM `oc_aa_affiliatecollclicktracking` AS e
LEFT JOIN `'.DB_PREFIX.'product_description` AS u ON e.product_id = u.product_id
GROUP BY e.product_id';
You want to perform a SUM command by group
$query = "SELECT `name`,SUM(`clicks`) FROM `mytable` GROUP BY `name`";
Edit: The other answer was more complete than mine. I forgot to select name field. Added.
What you need is an aggregate function for suming the clicks [SUM(clicks)], and a Group By clause for defining the classification criteria [GROUP BY name].
The other answers where wrong in the sense that are assuming that by changing the select field list adding the aggregate 'SUM', the associative references (eg: index strings of the $row array ) remains the same, the correct query would be:
$select_table2 = 'SELECT name, SUM (clicks) AS clicks FROM mytable GROUP BY name';
Note the alias on SUM(clicks) AS clicks, so the fields returned in the array $row keep their indexes (clicks, names... instead of 'SUM(clicks)')
And the rest is basically the same.
Cheers!
Try :
$select_table2 = 'SELECT name, SUM (clicks) FROM mytable GROUP BY name';
$query='SELECT * FROM #__virtuemart_products as a
LEFT JOIN #__virtuemart_products_en_gb as b ON a.virtuemart_product_id = b.virtuemart_product_id
INNER JOIN #__virtuemart_product_categories as c ON a.virtuemart_product_id=c.virtuemart_product_id
INNER JOIN #__virtuemart_categories_en_gb as d ON c.virtuemart_category_id = d.virtuemart_category_id
WHERE b.slug LIKE "'.$current.'%" AND a.product_parent_id = 0
AND d.category_name="'.$query_title.'"' ;
$db->setQuery($query);
$options=$db->loadObjectList();
This is the query i use to parse some products from my db. The problem is:
Table: virtuemart_products_en_gb has a collumn named slug
Table: virtuemart_categories_en_gb has also a collumn named slug
When i used $row->slug it parsed the virtuemart_categories_en_gb slug.
So after i var_dumped the ObjectList i see that there is only 1 collumn named slug. After i used the same query in phpmyadmin, it returns me 2 collumns named slug.
I think i could fix that selecting every single record individual and setting first slug as slug1 and second as slug2.
For example: SELECT id,username,password,b.slug as slug1,c.slug as slug2 etc
Is there any better way ? Cause i need to parse really many fields and that would make the query really huge.And why the php query returns only 1 field named slug while phpmyadmin returns both of em ?
Thanks in advance.
You can specify * after selecting the columns with the same name. For example,
SELECT *, b.slug as slug1,c.slug as slug2
FROM #__virtuemart_products as a
LEFT JOIN #__virtuemart_products_en_gb as b ON a.virtuemart_product_id = b.virtuemart_product_id
INNER JOIN #__virtuemart_product_categories as c ON a.virtuemart_product_id=c.virtuemart_product_id
INNER JOIN #__virtuemart_categories_en_gb as d ON c.virtuemart_category_id = d.virtuemart_category_id
WHERE b.slug LIKE "'.$current.'%" AND a.product_parent_id = 0
AND d.category_name="'.$query_title.'"
I am hoping for some help making my queries more efficient if possible. I have two tables. One which contains the types of content available and another which contains content linked to the type table by a type id.
I am trying to select from the content table and group the results by type_id. I am currently using a query inside a loop to do this so it is selecting again from the second table for each type_id. Is there a better/more efficient way to do this?
Here are my current tables and queries:
type_id type
1 type 1
2 type 2
id title type_id content
1 test1 1 test content 1
2 test2 2 test content 2
3 test3 1 test content 3
$query="select type_id, type from type_table";
foreach($query as $type){
$query="select title, content from content_table WHERE type_id=$type['type_id']";
}
I hope this makes sense.
Thanks
Your example appears to select all possible values from the content table, albeit in batches of a given type at a time. So select title, content from content_table would surely do this whole thing in one go, without looping.
To put each type_id into a new div, you could do something like this:
SELECT title, content, t.type_id, t.type
FROM content_table c
JOIN type_table t ON t.type_id = c.type_id
ORDER BY t.type_id
// pseudo-code
prev_type_id = -1
print "<div>"
foreach row:
if type_id != prev_type_id:
prev_type_id = type_id
print "</div><div>"
print "</div>"
(edited based on comments)
Use a join and order by. Something like (no guarantee for syntactical correctness):
SELECT
c.title, c.content, t.type_id, t.type
FROM
type_table t, content_table c
WHERE
c.type_id = t.type_id
ORDER BY
t.type_id
Sure, you could join the two tables.
In this example, however, it doesn't appear that you even need to use the type_table. You could just sort your query by type_id:
select title, content from content_table order by type_id;
What is the expected result ?
This is what I think you want.
$sql = 'SELECT t.type, c.title, c.content
FROM `content_table` c
INNER JOIN `type_table` t ON c.type_id = t.type_id
ORDER BY c.type_id ASC'
// Dont use a foreach-loop to loop over the results. While-loop is ideal
while ($row = <query or fetch from resultset here>) {
..
}
I'm trying to join two tables. The first table has a list of 11 items which are 'site_names' with an auto id field of 'id'. The second table that I want to connect has an auto id field of 'desc_id' and another field of 'descriptions'. This second table currently has 3 rows of data that I want displayed only for id 1 in table 1.
So, I want to accomplish is to connect the first site in table one with an id of '1' to the entire second table.
I can't seem to figure out how connect only the first entry(id=1) in table 1 to all the rows in table 2 (tb.1->id->1 to tbl.2->desc_id->1,2,3).
I hope that made sense. Any help would be great. Thanks
Try:
select site_name, descriptions
from table_1
inner join table_2
on 1 = 1
where table_1.site_id = 1
This should join give you what you want.
OK - based on the comment, I'm guessing what you want is:
site1 | desc1 | desc2 | desc3
all on one row. This is a bit trickier - particularly if you want it to remain open to an arbitrary number of descriptions. For just 3 (or, really, any limited subset, but as the number goes up, it gets ugly), you could do:
select site_name, t2.desc, t3.desc, t4.desc
from table_1
inner join table_2 t2
on t2.desc_id = 1
inner join table_2 t3
on t3.desc_id = 2
inner join table_2 t4
on t4.desc_id = 3
where site_id = 1
This kind of stuff is highly irregular though. It seems to me like something about your schema is probably not quite right to generate this sort of requirement.
Here is the query:
<?php
$mysql = new mysqli('localhost', 'root', 'root') or die('counld not connect');
$result = $mysql->query("SELECT ajax_demo.explore.site_name, anthony1.property.descriptions FROM ajax_demo.explore INNER JOIN anthony1.property ON ajax_demo.explore.id = anthony1.property.desc_id") or die($mysql->error);
if($result)
{
while($row = $result->fetch_object())
{
$id = $row->id;
$siteName = $row->site_name;
$siteDescription = $row->site_description;
echo "$siteName";
echo "$siteDescription";
}
}
?>
I may be missing something here, but it sounds to me like you need to add a foreign key to the Site table. If I understand your question correctly, your tables should look something like this:
Site
- SiteID
- DescriptionID
- SiteName
Description
- DescriptionID
- Description
Then your query to get Sites and their associated Descriptions would look like this:
SELECT
s.SiteName,
d.Description
FROM
Site s INNER JOIN Description d
ON s.DescriptionID = d.DescriptionID
This table structure assumes that multiple Sites share single Descriptions (as per your posted question).