using arrays to query with SELECT with PHP - php

I'm querying a table that returns id's and I'd like to use these id's within an array to perform another query.
I have the two queries working independently of each other by hard coding the id's into the second query just to be sure that it functions as expected, which it does.
$query = "SELECT item_id FROM items";
$query = "SELECT * FROM results WHERE results_item_id in (1,2,3,4)";
I'd like the return of the second query to include data for all of the id's returned to me from the first query.

Instead of sub query, you can do it by JOIN matching ON item_id = results_item_id
$query = "SELECT R.* FROM results R JOIN items I ON I.item_id = R.results_item_id";

Just put the query inside the IN, and add distinct so each item_id you only get in once:
$query = "SELECT * FROM results WHERE results_item_id in (SELECT distinct item_id FROM items)";

Related

How to assign two SELECT queries results to single array variable($row)?

There are two queries with same fields names but different tables. I want fetch both select query's results in same array variable $result. I tried few things but nothing working for me. May b my bad day.
two table, need to be fetch in same variable $result.
$sql1="SELECT city,phone,name from table1 where city='NY'";
$result = $conn->query($sql1);
$sql2="SELECT city,phone,name from table2 where city='NY'";
$result = $conn->query($sql2);
I don't want $result lost $sql1 data after assigning same variable ($result) to second query. Please help
You can use UNION and along with that table record identifier to identify table records differently.
$sql1 = "(SELECT city,phone,name, 't1' ttype from table1 where city='NY')
UNION (SELECT city,phone,name,'t2' ttype from table2 where city='NY')";
$result = $conn->query($sql1);
Note: MySQL uses the DISTINCT clause as default when executing UNION queries if nothing is specified.
If you want duplicate records too, then use UNION ALL.
$sql1="SELECT city,phone,name from table1 where city='NY'
UNION
SELECT city,phone,name from table2 where city='NY'";
$result = $conn->query($sql1);

how to write sub query child query with multiple row parent query single row return

The following query produces some errors:
$sub = '( SELECT mulitple.* FROM timezoneabbreviation AS mulitple
WHERE single.gmtoffset = mulitple.gmtoffset) AS sametimezone';
$query = "SELECT $sub,single.* FROM timezoneabbreviation AS single
WHERE full_name='".$full_name."' ";
return $this->db->query($query)->row();
I want to write a query with a subquery in the same table with where condition main query produces single row but sub query produces multiple rows.
Just use JOIN
$query = "SELECT mulitple.*, single.*
FROM timezoneabbreviation AS single
INNER JOIN timezoneabbreviation AS mulitple
ON mulitple.gmtoffset = single.gmtoffset
WHERE single.full_name='".$full_name."'";

Ordering by a value that is not in the database where selecting from

how would i go about ordering by a value that is not in the table where i am selecting from, in this instance the value $count1 is not in the table search.
count has the same identifying id as that of the thing it is being reffered to in the other table, this is where count1 is grabbed
$q = $db->prepare("SELECT COUNT(rating) FROM ratings WHERE id='$id' AND rating = 'd'");
$q->execute();
$count1 = $q->fetchColumn();
$query = "SELECT * FROM search WHERE title LIKE '$each' ORDER BY '$count1'"
$query = $db->prepare($query);
$query->execute();
that is from ratings, how would i go about ordering the entries like that, so that they are based off the number of count1 and are decided, i might have to implement something like
$query = "SELECT * FROM search WHERE title LIKE '$each' AND id = '$id' ORDER BY '$count1'"
$query = $db->prepare($query);
$query->execute();
Possible Duplicate: Mysql order by specific ID values
Same thing here, you'll just output your $count1as a comma separated string and add it in the SQL query as ORDER BY FIELD(COUNT,___comma_sep_string___)
ratings is a table, not a database. You can join tables or use subqueries to get the desired result, without having to make multiple queries.
You haven't described how the FOREIGN_KEY is set up in the ratings table, but assuming you have something ratings.search_id, this should work:
SELECT search.*, (SELECT COUNT(rating)
FROM ratings
WHERE ratings.search_id = search.id
AND rating = 'd'
) AS rating_count
FROM search
WHERE title LIKE '$each'
ORDER BY rating_count

MySQL two conditions for two tables

I have a mysql query:
$query5 = mysql_query("SELECT * FROM `pages` WHERE (`id`='$switch' AND `rand`='$randID' AND `email`!='".$_SESSION['user']."') ");
And second:
$query5 = mysql_query("SELECT * FROM `pages_admin` WHERE (`pId`='$switch' AND `rand`='$randID' AND `admin`!='".$_SESSION['user']."') ");
I use a while loop to present data.
while($row = mysql_fetch_array($query5)) {}
I need one mysql query instead two.
If these tables are related you can JOIN them using the foreign key.
If I'm not mistaken this pId in the table pages_admin is a foreign key to the id on the table pages, is that correct?
If so, you could do something like this to you query:
"SELECT * FROM pages p
LEFT JOIN admin_pages ap on p.id = ap.pId
WHERE (`pId`=$switch AND `rand`=$randID AND `admin`!='{$_SESSION['user']}')"
Note that I've changed the syntax, instead of merging string you can use only one containing all variables you need.

merge two query into one single query in sql

In my file there is already one query name query1 and I write another query name query2. The o/p of both query is userid. Now I want to find the common userid from these two queries. So is there any function for that?
The array_intersect() function generates an error.
Use a JOIN
$query1 = "SELECT userid FROM table1";
$query2 = "SELECT userid FROM table2 WHERE something = 10";
$joinQuery = "
SELECT a.userid
FROM ($query1) AS a
JOIN ($query2) AS b
ON a.userid = b.userid";

Categories