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."'";
Related
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.
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();
$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.
I don't know if these are "complex queries" by defn, but they look very complex to a noob like me.
So I have a query here that will get the latest chart of customer_id=5:
$query = "SELECT c.Chart_ID, c.Chart_Notes
FROM tblchart AS c WHERE c.Customer_ID=5
ORDER BY c.Last_Edited ASC LIMIT 1";
But I have to relate it to another table that uses the Chart_ID as foreign key. How can I get the data from the tblcontent using tblchart.Chart_ID=tblcontent.Chart_ID? I couldn't just add that as:
$query = "SELECT c.Chart_ID, c.Chart_Notes, d.Content_Desc, d.Content_Title
FROM tblchart AS c, tblcontent AS d
WHERE c.Customer_ID=5 AND c.Chart_ID=d.Chart_ID
ORDER BY c.Last_Edited DESC LIMIT 1";
can I? As that would limit the search to just one...the use of LIMIT 1 is just to get the latest, but for the subsequent query (extended query), I am expecting multiple results extracted from tblcontent in addition to the first query I posted. A join, maybe, or union, or a complex query, but how? Please, can anyone help me? Thanks.
SELECT a.Chart_ID, a.Chart_Notes, c.Content_Desc, c.Content_Title
FROM tblChart a
INNER JOIN
(
SELECT Chart_ID, MAX(Last_edited) maxEdited
FROM tblChart
GROUP BY Chart_ID
) b ON a.Chart_ID = b.Chart_ID AND
a.Last_Edited = b.maxEdited
INNER JOIN tblcontent c
ON a.Chart_ID = c.Chart_ID
WHERE a.Customer_ID=5
Hey guys need some more help
I have 3 tables USERS, PROFILEINTERESTS and INTERESTS
profile interests has the two foreign keys which link users and interests, they are just done by ID.
I have this so far
$statement = "SELECT
InterestID
FROM
`ProfileInterests`
WHERE
userID = '$profile'";
Now I want it so that it selects from Interests where what it gets from that query is the result.
So say that gives out 3 numbers
1
3
4
I want it to search the Interests table where ID is = to those...I just don't know how to physically write it in PHP...
Please help.
Using a JOIN:
Best option if you need values from the PROFILEINTERESTS table.
SELECT DISTINCT i.*
FROM INTERESTS i
JOIN PROFILEINTERESTS pi ON pi.interests_id = i.interests_id
WHERE pi.userid = $profileid
Using EXISTS:
SELECT i.*
FROM INTERESTS i
WHERE EXISTS (SELECT NULL
FROM PROFILEINTERESTS pi
WHERE pi.interests_id = i.interests_id
AND pi.userid = $profileid)
Using IN:
SELECT i.*
FROM INTERESTS i
WHERE i.interests_id IN (SELECT pi.interests_id
FROM PROFILEINTERESTS pi
WHERE pi.userid = $profileid)
You are on the right track, lets say you execute the query above using this PHP code:
$statement = mysql_query("SELECT InterestID FROM `ProfileInterests`
WHERE userID = '$profile'");
Then you can use a PHP loop to dynamically generate an SQL statement that will pull the desired IDs from a second table. So, for example, continuing the code above:
$SQL = "";
while ($statementLoop = mysql_fetch_assoc($statement)) {
//Note the extra space on the end of the query
$SQL .= "`id` = '{$statementLoop['InterestID']}' OR ";
}
//Trim the " OR " off the end of the query
$SQL = rtrim($SQL, " OR ");
//Now run the dynamic SQL, using the query generated above
$query = mysql_query("SELECT * FROM `table2` WHERE {$SQL}")
I haven't tested the code, but it should work. So, this code will generate SQL like this:
SELECT * FROM `table2` WHERE `id` = '1' OR `id` = '3' OR `id` = '4'
Hope that helps,
spryno724
Most likely you want to join the tables
select
i.Name
from
ProfileInterests p
inner join
interests i
on
p.interestid = i.interestid
where
p.userid = 1