I am trying to get data of 'indication' from two different tables.
The script works fine when selecting FROM number_one only.
Did try this with a , inbetween but that doesn't work.
How should I do this?
query2 = mysql_query("SELECT `indication` FROM `number_one`, `number_two` ORDER BY `indication` DESC");
while($row2 = mysql_fetch_object($query2)){
if($explode2[1] == $row2->indication){
echo "<option value=\"$row2->indication\" selected=\"selected\">$row2->indication</option>";
}
else{
echo "<option value=\"$row2->indication\">$row2->indication</option>";
}
}
Solution
query2 = mysql_query("SELECT `indication` FROM `number_one` UNION ALL SELECT `indication` FROM `number_two` ORDER BY `indication` DESC");
while($row2 = mysql_fetch_object($query2)){
if($explode2[1] == $row2->indication){
echo "<option value=\"$row2->indication\" selected=\"selected\">$row2->indication</option>";
}
else{
echo "<option value=\"$row2->indication\">$row2->indication</option>";
}
}
Assuming you have two tables, number_one and number_two with the indication column, your query would cross-join the two tables, giving you a set of rows which each have two columns (number_one.indication and number_two.indication, in all the possible combinations.
If I understand the question correctly, you want to have them as one column, with all the values from both tables. This can be done using the UNION ALL set operator, as I demonstrate below:
SELECT indication
FROM ( SELECT indication
FROM number_one
UNION ALL
SELECT indication
FROM number_two)
ORDER BY 1 DESC
Related
I am trying to read table data and display in on a table the only thing is that i am trying to read it on 2 tables. The 2 tables have the same structure and i have tried it with the code below but nothing happens.
$sql = "SELECT pricedatamt5.symbol_name, pricedatamt5.symbol_bid, pricedatamt5.symbol_ask, pricedatamt4.symbol_bid, pricedatamt4.symbol_ask FROM pricedatamt5 LEFT JOIN pricedatamt4 ON pricedatamt5.symbol_name = pricedatamt4.symbol_name WHERE symbol_name='".$value."'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row["symbol_date"]."</td>";
echo "<td>".$row["symbol_bid"]."</td>";
echo "<td>".$row["symbol_ask"]."</td>";
echo "</tr>";
}
I hope you can help me with this one because i am really having a hard time especially on retrieving data from 2 tables.
Thanks...
if you need the union your sql query will be:
$sql = "SELECT pricedatamt5.symbol_name, pricedatamt5.symbol_bid, pricedatamt5.symbol_ask FROM pricedatamt5 WHERE symbol_name='".$value."'"."
UNION
SELECT pricedatamt4.symbol_name, pricedatamt4.symbol_bid, pricedatamt4.symbol_ask FROM pricedatamt4 WHERE symbol_name='".$value."'";
otherwise you should clarify your question.
The code I'm running below runs really quite fine, until I try and fetch the Quantity in the last loop. I think it could probably be optimized with JOINs etc but I'm not overly familiar with the best approach for this when grabbing DISTINCT elements?
$sql_dept = "SELECT DISTINCT `department_guid` FROM `bom_material_copy` WHERE `complex_guid`='9EB75BE9-26E2-AEBB-1B52-37DBC2FB89EA' ORDER BY `id` DESC LIMIT 0,5";
foreach($pdo->query($sql_dept) AS $dept)
{
$sql_mat = "SELECT DISTINCT `material_bct_number` FROM `bom_material_copy` WHERE
`complex_guid`='9EB75BE9-26E2-AEBB-1B52-37DBC2FB89EA' AND
`department_guid`='".$dept['department_guid']."'
ORDER BY `id` DESC LIMIT 0,5";
foreach($pdo->query($sql_mat) AS $mat)
{
echo "<tr>";
echo "<td>".$mat['material_bct_number']."</td>";
// Grab Units Again
$u = 0;
$sql_unit = "SELECT DISTINCT `unit_number` FROM `bom_projects_units` WHERE `complex_guid`='9EB75BE9-26E2-AEBB-1B52-37DBC2FB89EA'";
foreach($pdo->query($sql_unit) AS $unit)
{
$sql_quan = "SELECT `quantity` FROM `bom_material_copy`
WHERE
`material_bct_number`='".$mat['material_bct_number']."' AND
`unit_number`='".$unit['unit_number']."' AND
`department_guid`='".$dept['department_guid']."'";
foreach($pdo->query($sql_quan) AS $quan)
{
echo "<td>".$quan['quantity']."</td>";
}
}
echo "</tr>";
}
}
Try using EXPLAIN in your queries.
When you do that, you know the relationship between tables that you're using in the query.
Next step, use INDEX in the fields that you're making match, and execute again you're queries.
I have 3 mysql tables
user(u_id(p),name),
team(t_id(p),u_id(f),t_name,t_money,days_money) and
history(t_id(f),day,d_money).
Now I have to display leaderboard using php.
I tried this.
SELECT t_id FROM team;
got result.
then,
in for loop
foreach($tid_all as $tid)
{
$que = $db_con->prepare("SELECT d_money, t_name FROM team, history WHERE t_id= '".$tid['t_id']."' && day='1'");
$que->execute();
while($info = $que->fetch(PDO::FETCH_NUM))
{
echo "<tr>";
echo "<td>".$info[0]."</td>";
echo "<td>".$info[1]."</td>";
echo "</tr>";
}
}
but it didnt work. any solution?
Solution 1:
i tried this and it worked.
`SELECT d_money, t_name FROM team, history WHERE history.t_id=$tid['t_id'] AND team.t_id=history.t_id`
is it correct way or not?
thanks everyone for help.
Question : is it possible to order the result table by d_money? i want it in descending order.
Replace && with AND.Try like this :
"SELECT d_money, t_name FROM team, history WHERE t_id= '".$tid['t_id']."' AND day='1' order by d_money DESC "
There is no && in MySQL Query. Replace that with AND Operator on your query.
Since you want to get the data from the two tables, then JOIN the two tables instead of doing that with a loop:
SELECT
h.d_money,
t.t_name
FROM team AS t
INNER JOIN history AS h ON t.t_id = h.t_id;
Run this single query once and you will get what you want. You can also add a WHERE clause at the end of it the way you did in your query.
try this
SELECT d_money, t_name FROM team, history WHERE team.t_id= '".$tid['t_id']."' AND history.t_id= '".$tid['t_id']."' && day='1'
Can you replace
WHERE t_id= '".$tid."' AND day='1'
instead of
WHERE t_id= '".$tid['t_id']."' && day='1'
I am creating a simple SQL query in PHP - and for some reason, even when DISTINCT is used, it shows twice like this:
BC
BC
OH
OH
TX
TX
Here is my code:
<?php
$sql = "SELECT DISTINCT `title`,`extra_fields_search` FROM `blahblah_items` WHERE catid=336 ORDER BY `blahblah_items`.`extra_fields_search` ASC ";
$partnerlisting= mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($partnerlisting))
foreach($row as $cname => $cvalue){
echo '<li>'.substr($row[extra_fields_search], 0, 2).'</li><br>';
}
;
?>
How can I make it so it prints out only one of each?
SELECT
`title`,
`extra_fields_search`
FROM
`blahblah_items`
WHERE
catid=336
GROUP BY
SUBSTRING(extra_fields_search,1,2) # here. group by first two characters of extra_fields_search
# or just "GROUP BY extra_fields_search", depends what you need
ORDER BY
`blahblah_items`.`extra_fields_search` ASC
SELECT DISTINCT a,b FROM table works in same way as SELECT a,b FROM table GROUP BY a,b
Please follow documentation:
http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Distinct make sure that you get unique rows. It does not make sure you will get unique column values. In your case if you consider all the fields in the result, you will notice that each row is different from the other by at least one field.
SO you will never get
Col1 Col2
A B
A B
But you can get
Col1 Col2
A B
A C
Try:
SELECT `title`, group_concat(distinct `extra_fields_search`)
FROM `blahblah_items`
WHERE catid=336
Group BY `title`
ORDER BY 2
try this
SELECT `title`,`extra_fields_search`
FROM `blahblah_items` WHERE catid=336
Group BY extra_fields_search
ORDER BY `blahblah_items`.`extra_fields_search` ASC
edit :
try this in your code
while($row = mysql_fetch_assoc($partnerlisting))
$rows[] = $row;
foreach($rows as $row){
echo '<li>'.substr($row[extra_fields_search], 0, 2).'</li><br>';
}
;
Hello I would like to query multiple identical tables in my db which has different prefixes and than display the results randomly but somehow I need to track the origin of the item and I couldn't figure out how
I do the query like this because I don't have access to information_schema
$query = "SHOW TABLES FROM mydb WHERE RIGHT( tables_in_mydb, 5 ) = 'table'";
$res = mysql_query($query);
$num = mysql_num_rows($res);
while($row = mysql_fetch_row($res)) {
$numbers = explode('_', $row[0]);
if($num > 0) {
$q = "SELECT `this`, `that`, `something` FROM ".$numbers[0]."_idetinticaltables"; // :)
$r = mysql_query($q);
while($c = mysql_fetch_array($r)) {
/*display the results randomly with an identifier where the come from*/
}
}
}
You could use ORDER BY RAND() to randomly sort it
The following might work:
Get the list of the tables you're interested in. You already do that.
Create a UNION of multiple SELECT statements. Each SELECT statement differs for the table being selected from and you add a column set to the name of the table (so you can identify it later):
(SELECT *, TABLENAME = 'first_name_of_table' FROM first_name_of_table ...)
UNION
(SELECT *, TABLENAME = 'second_name_of_table' FROM second_name_of_table ...)
UNION
...
ORDER BY RAND() LIMIT 10;
Because it is a UNION you can randomize the whole order then. See How can i optimize MySQL's ORDER BY RAND() function? because it is not that trivial to do well, the example above is only to have an ORDER BY and LIMIT clause placed there. With many entries in your tables, it will kill your server.
$aa=array()
while($c = mysql_fetch_array($r))
{
/*display the results randomly with an identifier where the come from*/
$aa[]=$c;
}
echo $aa; // print "Array"