Identifying specific table after mysql UNION - php

I have this code
$query = mysqli_query($con, "SELECT id, dateAdded as date FROM table1 UNION SELECT id, dateSeen as date FROM table2 ORDER BY date DESC");
While($sql = mysqli_fetch_array($query)){
$id = $sql['id'];
}
Please how can i detect from which table an $id originated. For instance if outputted I get something like this
1, 2, 1, 3, 2, 4, 3, 4, 5, 6
So how can I know from which table each id came from because I have other informations I want to retrieve from each separate table using a function, Example
$title = table_query('title', $table, $id);
$name = table_query('name', $table, $id);

You can't really do this. What you can do is add data in your query:
$query = mysqli_query($con, "SELECT id, dateAdded as date, 'table1' AS tblname FROM table1 UNION SELECT id, dateSeen as date, 'table2' AS tblname FROM table2 ORDER BY date DESC");
while($sql = mysqli_fetch_array($query)){
$id = $sql['id'];
$tbl = $sql['tblname']; // this variable will store the name of the table.
}
For each query, the string table1 | table2 will be added as the 3rd column, and you can use it in your code.

Related

get multiple table with same columns data

i have three table with same value of columns. example: say i have three table "table1", "table2", "table3" and each columns has "id", "title", "description", "category", "date", "thumbnail", "admin". now i'm trying to get all data from those three table. but there is a think, i want to check with if statement. if table1 not match with id, check table2. if table2 not match with id, check table3 and at last show the data. please check my below code, i'm trying to get data from those three table:
<?php
include('config/database.php');
$id=$_GET['single'];
$query=mysqli_query($conn,"select * from table1, table2, table3 where id='$id' ");
while($row=mysqli_fetch_array($query)){
$title=$row['title'];
$date=$row['date'];
$admin=$row['admin'];
$thumbnail=$row['thumbnail'];
$description=$row['description'];
$category=$row['category'];
}
?>
please help me to get all data from those three table with if statement
it will be better to understand if you post an answer. thank you in advance.
Use a UNION of 3 queries.
$sql = "
SELECT * FROM (
SELECT 1 AS tnum, * FROM table1 WHERE id = ?
UNION ALL
SELECT 2 AS tnum, * FROM table2 WHERE id = ?
UNION ALL
SELECT 3 AS tnum, * FROM table3 WHERE id = ?
) AS x
ORDER BY tnum
LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param('iii', $id, $id, $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row) {
$title = $row['title']
$date=$row['date'];
$admin=$row['admin'];
$thumbnail=$row['thumbnail'];
$description=$row['description'];
$category=$row['category'];
}
Adding the tnum column to the result orders them so the table1 data is preferred, then table2, finally table3.

compare strings in two different tables

i have two tables with two columns:
Table1 table1
Columns id, address1
Table2 table2
Columns id, address2
I just wont to compare the addresses in the column address1 with column address2 to find out duplicate addresses. The table2.address2 contains some addresses from table1.address1
If there is a match then, make an update for example on the table2 column match put 1 else put nothing...
Column match is just an example!!!
here is what i have:
// table 1
$query = "SELECT id, address1 FROM table1";
$sqldata = mysql_query($query);
while ($row = mysql_fetch_array($sqldata, MYSQL_BOTH) ) {
$kr_id = $row['id'];
$address1 = $row['address1'];
}
// table 2
$query = "SELECT id, address2 FROM table2";
$sqldata2 = mysql_query($query);
while ($row2 = mysql_fetch_array($sqldata2, MYSQL_BOTH) ) {
$id = $row2['id'];
$address2 = $row2['address2'];
if ($row2[$address2] == $address1) {
// make an SQL - Update
}
}
thans for any help!!!
You can use update with join:
UPDATE Table2 t2
INNER JOIN Table1 t1
ON(t1.mk_adress = t2.adress)
SET t2.match = 1
You may have to adjust the column names a little bit, couldn't figure out which table you want to be updated.

fetch multiple value from same table using id value

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.

Joining two tables to get a count

I am attempting to count comments on a particular page with the following problematic sql query:
$query = "SELECT * FROM `comments` WHERE is_approved = '1' AND page_id = '943'"
$query = mysql_query($query);
$total = mysql_num_rows($query);
echo $total;
the problem is it is outputting 0 and not 2.
The tables are as follows:
pages:
id:1 page_id:943
id:2 page_id:978
id:3 page_id:977
comments:
id:2 page_id:1 "hello"
id:3 page_id:1 "great"
id:4 page_id:3 "super"
So really the original query should be getting each comment's true page_id from the page_id as set in the pages tables, as joined by comments.page_id = pages.id
What would the final code look like to either make that join, and/or get that count? Thank you.
Try:
SELECT c.* FROM `comments` c
JOIN `pages` p on c.page_id = p.id
WHERE c.is_approved = '1' AND p.page_id = '943'
"SELECT * FROM comments, pages WHERE comments.page_id = pages.id AND is_approved = '1' AND comments.page_id = '943'"
Try using:
SELECT count(*) as cnt
FROM `comments` c join pages p on c.page_id = p.id
WHERE c.is_approved = '1' AND p.page_id = '943'
It seems like a very poor database design to have two columns with the same name in different tables that mean different things. You should probably change the name of pages.page_id to something else.
And, this returns the count directly, so you can read the value from the row. If you just want the count, there is no reason to return all the matching rows.
no join is needed:
$query = "SELECT * FROM `comments` WHERE is_approved = '1' AND WHERE page_id IN (SELECT id WHERE page_id = '943')"
$query = mysql_query($query);
$total = mysql_num_rows($query);
echo $total;
ofcourse i would suggest a count statement if you do not need/use the data:
$query = "SELECT COUNT(*) as total FROM `comments` WHERE is_approved = '1' AND WHERE page_id IN (SELECT id WHERE page_id = '943')"
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$total = $row['total'];
echo $total;

How to get information from 2 tables at once in PHP and MySQL?

<?php
include('includes/config.php');
$topi = $_GET['id']; //id of url
mysql_select_db("ban", $con);
$query = "SELECT * FROM `basic` WHERE id = '$topi' LIMIT 0, 30";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$aa = $row['item'];
$cc = $row['moreinfo'];
$dd = $row['contactinfo'];
$ff = $row['id'];
In this script, I get information from the table basic, but I want to retrieve data from another table named users. How can I retrieve data from two tables at once?
users table consists of following columns:
email
username
ID
You need to JOIN the two tables on a common value, called a foreign key. Once you've posted the structure of the users table as requested in the comments, I can provide a more complete example.
EDIT: See example. This calls explicit column names instead of SELECT *.
$query = "SELECT
basic.id,
basic.item,
basic.moreinfo,
basic.contactinfo,
users.email,
users.username
FROM basic JOIN users ON basic.id = users.id
WHERE id = '$topi'
LIMIT 0 , 30";
You would use a JOIN onto the other table.
$query = "SELECT *
FROM basic b
JOIN users u ON b.user_id = u.user_id
WHERE id = '$topi'
LIMIT 0, 30";
Something like that, but based on your fields.
Please Note: the ON clause specifies what you will be looking for a match on.

Categories