I implemented my project in Yii. I wrote query for single table like query with condition.
I want to write query to call from the two different table. I've tables are recipe and ingredient table. Recipe table I've more than 7 fields. recipe_id, cuisinename, course_id, type etc. another table ingredient. id, ingredient_name.
$result="SELECT * FROM recipe WHERE name LIKE '%$name%' AND `cuisinename` LIKE
'$cuisine1%' AND course_id LIKE '%$course1%' AND `type` LIKE '%$type1%'
AND `calorie_count` LIKE '$calorie1%' ORDER BY recipe_id DESC";
I wrote this condition. my search function is working very well. But I want to display from the other table i.e. ingredient table also.
could I write like query??
Sounds like you need a JOIN. However I would imagine Recipe and Ingredients would have a many to many relationship in your database. You'd need to first make an associative table for them and then use an INNER JOIN to link up the 3 tables.
Something like this:
SELECT /* you stuff */
FROM recipe_ingredients ri
INNER JOIN recipe r ON ri.recipe_id = r.id
INNER JOIN ingredients i ON ri.ingredient_id = i.id
WHERE /* do you like stuff here */
Further to my comment, you could try this;
/* first query */
$query = "SELECT * FROM recipe WHERE name LIKE '%$name%' AND `cuisinename` LIKE '$cuisine1%' AND course_id LIKE '%$course1%' AND `type` LIKE '%$type1%' AND `calorie_count` LIKE '$calorie1%' ORDER BY recipe_id DESC";
$result = $mysqli->query($query);
/* numeric array */
$ret1 = $result->fetch_array(MYSQLI_NUM);
/* second query */
$query = "Whatever is in your second query?"; //add your second query here..
$result = $mysqli->query($query);
/* numeric array */
$ret2 = $result->fetch_array(MYSQLI_NUM);
/*final result set */
$result = array_merge((array)$ret1, (array)$ret2);
If you are using Yii, use query builder http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder
Query will look like
Yii::app()->db->createCommand()
->select('*, i.ingridient_name')
->from('recipe t')
->join('ingredients i', 'i.recipe_id=t.id')
->where('name LIKE :name AND `cuisinename` LIKE
:cuisine1 AND course_id LIKE :course1 AND `type` LIKE :type1
AND `calorie_count` LIKE :calorie1', array(
':name'=>'%'.$name.'%',
':cuisine1'=>'%'.$cuisine1.'%',
))
->order('recipe_id DESC')
->queryAll();
Related
Have two SQLite tables:
sh_item
id
title
..
and
sh_paramvalues
id
paramid
itemid
value
Have some query that I need to modify
//$get[0] - search query from user
$this->q = "SELECT *
FROM sh_item
WHERE title LIKE '%".$get[0]."%'
OR descr LIKE '%".$get[0]."%'
LIMIT '".$this->page->start."','".$this->page->on1page."'";
I need some query that could get value ( LIKE '%".$get[0]."%') from sh_paramvalues where paramid='some my value' and itemid = id which use sh_item in base query.
Maybe I need to use join but I am not good at that.
Try:
//$get[0] - search query from user
$this->q = "SELECT *
FROM sh_item si
INNER JOIN sh_paramvalues sp
ON si.id = sp.itemid
WHERE (si.title LIKE '%".$get[0]."%'
OR si.descr LIKE '%".$get[0]."%'
OR sp.value LIKE '%".$get[0]."%')
AND sp.paramid = 3
LIMIT '".$this->page->start."','".$this->page->on1page."'";
I want to fetch value Rows from single table.I want to fetch sub_id for specific id.
I achieved my require ment in 2 query.I want to do it in single query.I want to display result as Event,order history,Eent Ticket,calander
$sql="select * from table1 where roles like %admin% and sub_id='0'"
$sql1=mysql_query($sql);
while($fet=mysql_fetch_assoc($sql1))
{
$id=$fet['id'];
$query="select page_name from table1 where sub_id= '$id'";
.. ..
}
Use a JOIN
SELECT t1.id, t1.sub_id, t1.page_name, t2.page_name AS parent_page
FROM table1 AS t1
JOIN table1 AS t2 ON t1.sub_id = t2.id
WHERE t2.roles like '%admin%' AND t2.sub_id = '0';
DEMO
use this
$sql="select sub_id from table1 where id='".$id."' ";
After this, use results of this as below
$sql= "select * from table1 where roles like %admin% and sub_id in($ids)";
You dont need another query to get the value of page_name just use $fet['page_name']; you already get the data of page_name in your first query.
$sub_id = $fet['sub_id'];//
echo $sub_id;//
$page_name = $fet['page_name'];//You can get and use the value of page_name here
UPDATED
if you want Event,Order History,Event Ticket and Calander
then change your where to sub_id = '2' ordered by id ascending.
$sql="select * from table1 where roles like %admin% and sub_id='2' order by id asc"
$sql1=mysql_query($sql);
while($fet=mysql_fetch_assoc($sql1))
{
echo $fet['page_name'].'<br/>';//display the page_name
}
You can also use the single query for getting page_name:
SELECT page_name FROM table1
WHERE roles LIKE %admin%
AND sub_id = 2
you can get Event,Order History,Event Ticket,Calendar as:
$sql="SELECT page_name FROM table1
WHERE roles LIKE %admin%
AND sub_id = 2";
$sql1=mysql_query($sql);
$records = array();
while($fet=mysql_fetch_assoc($sql1))
{
$records[] = $fet['page_name'];
}
echo implode(",",$records); // Event,Order History,Event Ticket,Calendar
UPDATE 1:
use sub_id = 2 for getting all page_name related to Event MAnagement
Side note:
I suggest you to use mysqli_* or PDO, instead of mysql_* because mysql_* is deprecated in not available in PHP 7.
$sql="select sub_id from table1 where id='".$id."' ";
if thats what you want.
$result = mysqli_query($db, "SELECT * FROM 'category' JOIN 'post-item' ON category.category-id = post-item.category-id");
1 post-item can have 1 category and 1 category can have many post-item.
Change
SELECT * FROM 'category'
JOIN 'post-item' ON category.category-id = post-item.category-id"
to
SELECT * FROM `category`
JOIN `post-item` ON `category`.`category-id` = `post-item`.`category-id`
You have single quote which is not correct
You need to specify particular fields because with * it cannot recognize which fields to fetch and from which table.
So do it something like
"SELECT table1.field1, table1.field2, table2.field4 FROM category JOIN post-item ON category.category-id = post-item.category-id"
Also you can leave out ` or ' while querying through PHP except when you are supplying dynamic variable.
Solution:
$query = $this->db->query("
SELECT *, SUM(views.times) AS sum
FROM channel
RIGHT JOIN video
ON channel.channel_id = video.channel_id
LEFT JOIN user
ON channel.user_id = user.user_id
LEFT JOIN views
ON channel.channel_id = views.channel_id
GROUP BY channel.channel_name
ORDER BY sum DESC
");
I have a query that returns a list of items.
function getfrontpage(){
$this->db->select('*');
$this->db->from('channel');
$this->db->join('video', 'channel.channel_id = video.channel_id' , 'right');
$this->db->join('user', 'channel.user_id = user.user_id');
$this->db->group_by("channel.channel_name");
$query = $this->db->get();
return $query->result();
}
Now i'm trying to order these with the SUM from another table, how can i achieve this?
Here is a picture from that table:
I want the results to be sorted by the SUM of the total of "times" for each "channel_id"
thanks in advance
I would suggest to run this through $this->db->query() instead.
It's nice to fetch simple values through CodeIgniters AR functions. But at some situations it's simply easier to build query strings instead.
In your case:
$query = $this->db->query("
SELECT channel_id, SUM(times) AS sum
FROM channel
GROUP BY channel_id
ORDER BY sum DESC
");
You can also escape most values through db->query()!
$this->db->query("
SELECT name
FROM table_name
WHERE id = ?
", array(1));
Isn't it as simple as $this->db->order_by("channel_id", "desc");? this orders the results by channel_id in descending order.
Assuming the table displayed in your question is called times_table, and has a key of user_id, channel_id, you can use the following code to join the times_table into your query so the "times" column is available to sort by.
$this->db->join("times_table", "times.user_id=channel.user_id, times.channel_id=channel.channel_id", "left");
// You've already grouped by channel_name, so grouping by channel_id is probably not necessary.
$this->db->order_by("SUM(times_table.times) DESC");
N.B. I just guessed the name of your displayed table is times_table.
Sorry let me revise. I have a three tables:
events_year
• EventID
• YearID
• id
Date
• YearID
• Year
Event
• EventID
• EventName
• EventType
i want to dispay a record from the three tables like so:
EventName - Year: Marathon - 2008
i linked it to a table called "members" which contains a ID number field (members-id)
so i can limit the results to members id = $un(which is a username from a session)
I need to join the three tables and limit the results to the specific ID number record
Here is my portion of the code:
$query = "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
"SELECT * FROM Event JOIN events_year ON Event.EventID = events_year.EventID WHERE username = '$un'";
"SELECT * FROM Date JOIN events_year ON Date.YearID = events_year.YearID WHERE username = '$un'";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo $row['Year'];
echo " - ";
echo $row['Event'];
echo "<br>";
}
the notices are almost self-explaining. There are no 'Year' and 'EventName' fields in the resultset. It's difficult (or: impossible) to tell why this happens as you haven't given your table-structure, but i guess this: 'Year' is a field of the date-table, 'EventName' is a field of the event-table - you're only selecting from members so this fields don't occur.
I don't understand why there are three sql-statements but only one is assigned to a variable - the other two are just standing there and do nothing. Please explain this and put more information into your question about what you're trying to achive, what your table-structure looks like and whats your expected result.
I think what you really wanted to do is some kind of joined query, so please take a look at the documentation to see how this works.
finally, i think your query should look like this:
SELECT
*
FROM
members
INNER JOIN
events_year ON members.id = events_year.id
INNER JOIN
Event ON Event.EventID = events_year.EventID
INNER JOIN
´Date´ ON ´Date´.YearID = events_year.YearID
WHERE
members.username = '$un'
Does the field 'Year' exist in the query output ? I suspect not.
the string $query is only using the first line of text:
"SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
and not the others.
The query itself is not returning any fields that are called Year or EventName.
Do a var_dump($row) to find out what is being returned.