I have a result set and need to be displayed as it is on the screen.But the problem is while displaying each row of the result set using echo command the order is getting changed.Can anyone say why this is happening and provide me a way to overcome it.Here are my actual and printed outputs.
Actual Result set:
JAIKE-ILENE-WACKI-MAZIE-REGLE-SBJ-KMMU
LVZ-HARTY-MUGZY-STW
MAZIE-SIXIE-SBJ-KMMU
PXT-LOUIE-GATBY-RAZER-BUZIE-JAIKE-ILENE-WACKI-MAZIE
SWANN-GATBY-RAZER-BUZIE-JAIKE-ILENE-WACKI-MAZIE
Output:
JAIKE-SBJ-ILENE-KMMU-WACKI-MAZIE-REGLE
MUGZY-STW-LVZ-HARTY
SBJ-KMMU-MAZIE-SIXIE
ILENE-GATBY-WACKI-RAZER-MAZIE-BUZIE-PXT-JAIKE-LOUIE
WACKI-RAZER-MAZIE-BUZIE-JAIKE-SWANN-ILENE-GATBY
Here is my code
$sql3="SELECT GROUP_CONCAT(l.fix_ident SEPARATOR '-') AS fix_seq,l.airport_ident,x.star_ident,x.transition_ident,
x.fix_ident from corept.std_star_leg l
JOIN
(SELECT DISTINCT c.airport_ident,c.star_ident,c.transition_ident,c.fix_ident
FROM corept.std_star_leg c
INNER JOIN
(SELECT star_ident,transition_ident,max(sequence_num) seq,route_type
FROM corept.std_star_leg
WHERE data_supplier='J'
AND airport_ident='KMMU'
GROUP BY star_ident,
transition_ident)b ON c.sequence_num=b.seq
AND c.star_ident=b.star_ident AND c.transition_ident=b.transition_ident
LEFT JOIN
(SELECT name,trans
FROM skyplan_deploy.deploy_stars
WHERE apt='KMMU'
AND name!=trans) d
ON d.name=c.star_ident
AND d.trans=c.fix_ident
WHERE c.data_supplier='J'
AND c.airport_ident='KMMU' AND d.name is null)x
where l.airport_ident='KMMU' and l.transition_ident=x.transition_ident
and l.star_ident=x.star_ident and l.data_supplier='J'
group by x.star_ident,x.transition_ident
order by l.star_ident,x.transition_ident,l.sequence_num";
$res3=mysqli_query($mysqli,$sql3);
if($res3)
{
while($newArray3=mysqli_fetch_array($res3,MYSQLI_ASSOC))
{
$apt=$newArray3['airport_ident'];
$star_ident=$newArray3['star_ident'];
$trans_ident=$newArray3['transition_ident'];
$fix_ident=$newArray3['fix_ident'];
$fix_seq=$newArray3['fix_seq'];
echo $apt.",".$star_ident.",".$trans_ident.",".$fix_ident.",COREPT,".$fix_seq;
echo "<br>";
}
}
else
{
printf("ERROR:%s\n",mysqli_error($mysqli));
}
Your query looks overly complex. It appears to be finding the groupwise maximum std_star_leg records by sequence_num (grouped on start_ident and transition_ident), excluding those for which there is already a matching non-self-referencing deploy_star, then returning the results grouped again with all matching fix_ident values concatenated into a string?
If so, the following greatly simplified query ought to achieve the same outcome:
SELECT GROUP_CONCAT(fix_ident SEPARATOR '-') AS fix_seq,
airport_ident,
star_ident,
transition_ident
FROM corept.std_star_leg l NATURAL JOIN (
SELECT star_ident, transition_ident,
data_supplier, airport_ident,
MAX(sequence_num) sequence_num
FROM corept.std_star_leg
WHERE data_supplier = 'J'
AND airport_ident = 'KMMU'
GROUP BY star_ident, transition_ident
) b
WHERE NOT EXISTS (
SELECT NULL
FROM skyplan_deploy.deploy_stars d
WHERE d.name != d.trans
AND d.name = l.star_ident
AND d.trans = l.fix_ident
AND d.apt = l.airport_ident
)
GROUP BY star_ident, transition_ident
Note that whereas you were previously selecting x.fix_ident in the outermost select list, I have omitted such column because its value would be indeterminately selected by the server from amongst those in the fix_seq.
Now, as to your problem (which appears to be related to the order in which fix_ident values appear within the GROUP_CONCAT() string fix_seq—although it's terribly hard to appreciate that from your question), perhaps you want to use the ORDER BY parameter to the GROUP_CONCAT() function? For example:
SELECT GROUP_CONCAT(fix_ident SEPARATOR '-' ORDER BY ...) AS fix_seq
However, it is not clear to me what ordering you require (the only ORDER BY clause in your original query was entirely redundant).
Related
include '../incs/connect.php';
$q=mysqli_query($con,"SELECT appl_reg_details.appl_id, appl_reg_details.apl_name, appl_reg_details.apl_email, rp_test_result.appl_status, rp_intv_result.intv_obt_marks
FROM appl_reg_details
INNER JOIN rp_test_result ON appl_reg_details.appl_id = rp_test_result.appl_id
INNER JOIN rp_intv_result ON rp_test_result.appl_id = rp_intv_result.appl_id
GROUP BY appl_reg_details.appl_id
");
WHILE($row=mysqli_fetch_array($q))
{
echo $row=['appl_id']." - ";
echo $row=['apl_name']." - ";
echo $row=['intv_obt_marks']." - ";
echo $row=['appl_status']." -";
echo $row=['apl_email']."<br/>";
}
I have 3 tables and i want to get data of 3 field from table1, of 1 field from table 2 and table 3 each, i have found that inner join can do this, when i run this it says 'Notice: Array to string conversion',
help to correct this or give a new way please
The query looks fine at first glance.
The problem is with the lines below it. They include an unnecessary =:
echo $row=['apl_email']."<br/>";
These lines should be like this:
echo $row['apl_email']."<br/>";
Performance benefits aside, GROUP BY serves no purpose here other than to confuse the result set. Try something like this instead, and fix the coding errors identified by rjdown:
SELECT DISTINCT d.appl_id
, d.apl_name
, d.apl_email
, t.appl_status
, i.intv_obt_marks
FROM appl_reg_details d
JOIN rp_test_result t
ON t.appl_id = d.appl_id
JOIN rp_intv_result i
ON r.appl_id = i.appl_id;
I am trying to return all the results from table one, AKA ship_skill_tree, while matching up the rows found in table two, AKA character_sheet_skills, even if the rows do not exist in table two.
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID`
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176 AND c.`character_id` = 1;
Table One Data:
|shipTypeID|shipGroupID|skillTypeID|skillLevel
______________________________________________
|11011|26|3332|1
|11129|31|3327|1
|11132|31|3327|1
|11134|31|3327|1
|11172|830|3328|5
|11172|830|12093|1
|11174|893|3328|5
|11174|893|28615|1
|11176|831|3330|5
|11176|831|12092|1
Table Two Data:
|character_id|typeID|skillpoints|level|published
______________________________________________
|1|3300|1415|2|1
|1|3301|8000|3|1
|1|3327|256000|5|1
|1|3330|2829|2|1
|1|3340|181020|4|1
|1|3341|1024000|5|1
|1|3342|32000|3|1
|1|3343|32202|3|1
|1|3380|256000|5|1
|1|3385|256000|5|1
|1|3386|256000|5|1
|1|3392|256000|5|1
|1|3394|90514|4|1
|1|3402|256000|5|1
|1|3410|768000|5|1
|1|3411|135765|4|1
|1|3412|750|1|1
|1|3413|256000|5|1
|1|3416|45255|4|1
|1|3417|0|0|1
|1|3418|0|0|1
|1|3419|135765|4|1
|1|3420|181020|4|1
|1|3423|0|0|1
|1|3425|90510|4|1
|1|3426|45255|4|1
|1|3428|500|1|1
|1|3429|8000|3|1
|1|3436|45255|4|1
|1|3437|45255|4|1
|1|3438|500|1|1
|1|3449|256000|5|1
|1|3453|0|0|1
|1|3455|256000|5|1
|1|3456|226275|4|1
|1|11579|271530|4|1
|1|12186|0|0|1
|1|12187|0|0|1
|1|12188|0|0|1
|1|12190|22547|3|1
|1|12191|45255|4|1
|1|12192|45255|4|1
|1|12193|45255|4|1
|1|12195|45255|4|1
|1|16281|256000|5|1
|1|17940|1024000|5|1
|1|20342|1280000|5|1
|1|22551|40000|3|1
|1|22578|181020|4|1
|1|25739|0|0|1
|1|26252|16000|3|1
|1|26253|750|1|1
|1|26261|750|1|1
|1|32918|16000|3|1
invTypes table:
|typeID|typeName
________________
|3327|Spaceship Command
|3328|Gallente Frigate
|3330|Caldari Frigate
|3332|Gallente Cruiser
|12092|Interceptors
|12093|Covert Ops
|28615|Electronic Attack Ships
In the above query shipTypeID will always, or should always, be valid and match a record in table one, however, in table two, the rows that match may not exist. What I need is to output as follows:
|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
|NULL|1|Interceptors
Currently this is what is returned:
|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
EDIT: Solution!
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
INNER JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176
You need to put any restrictions on the table being joined in the ON clause. If you put them in the WHERE clause it doesn't work, because the rows that don't have any matches will produce NULL for those columns, and the WHERE clause will filter them out.
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176
DEMO
You need to use a right join or an outer join rather than a left join. Have a look through the Visual Representation of SQL Joins for a good overview
Fiddle with tables here
I'm using the following sql with the tables in the fiddle to check if a user has reached the borrowing limit. The problem here is, If an invalid item number were supplied it returns NULL, if a user has not borrowed any items, it returns NULL. This way, I cannot tell if a invalid item number were supplied or if a user actually has not borrowed any books. What would be a good way to check if a invalid item number was supplied or a member actually has not borrowed anything under that category?
set #mId = 3 //Has not borrowed anything till now.
set #id = 21; //This item does not appear in the collection_db table and is therefore invalid.
set #country = 'US';
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder and col1.country = lend.country
WHERE col1.id = #id and col1.country = #country
AND col2.category = 10
AND lend.memId = #mId and lend.country = #country
The furthest I could get with the one query is (had to take out php and "country" vars for fiddle to work):
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
,case when valid1.id is not null then 'true' else 'false' end as validId
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder,(
Select Distinct a.id From collection_db a
Where a.id = 4) valid1
WHERE col1.id = 4
AND col2.category = 10
AND lend.memId = 1
You may have to do a preparatory query checking for a valid memId:
$theQuery = "SELECT DISTINCT memId FROM lendings WHERE memId = 1"
Then test it here:
if (mysql_num_rows(mysql_query($theQuery)) <= 0) { /* No memId exists */ }
else { /* Do big query here */ }
You can use a tableA LEFT JOIN tableB, which will return results for the tableA even if tableB has no matches and will return NULL values for those in tableB.
Unfortunately, I can't quite figure out where you need LEFT JOINS, but probably you want them in both places.
You also might have to reorder the tables if it is the first table that should be on the right side of a LEFT JOIN. You could use a RIGHT JOIN but it is less readable to me.
maybe you should try "left join" if col1 do not have too much data,or do the query step by step
I have used this code to get definitions:
//assuming you've connected to your MySQL db
$word=$_GET['s']; //This variable stores the value given through url
if (ctype_alpha($word)){ // If it's alphabetical
$word_clean=mysql_real_escape_string($word); //Sanitize it for MySQL
}else{
//Not a valid word, error handle
exit();
}
$query='SELECT wordno FROM word WHERE lemma=`$word_clean` LIMIT 1';
$result=mysql_query($query);
$query='SELECT synsetno FROM sense WHERE wordno=`$wordno`';
$query='SELECT definition FROM synset WHERE synsetno=`$synset`';
BUT NOW I want to get synonyms and similar of that word ($word);
SELECT a.lemma, c.definition
FROM word a
INNER JOIN sense b
ON a.wordno = b.wordNo
INNER JOIN synset c
ON b.synsetno = c. synsetno
WHERE a.lemma = 'valueHere'
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
A fast way to get a synonym/similar word using wordnet v3.0 with SQL:
SELECT w.lemma
FROM words AS w
LEFT JOIN senses AS s ON s.wordid = w.wordid
LEFT JOIN senses AS s2 ON s2.synsetid = s.synsetid
LEFT JOIN words AS w2 ON w2.wordid = s2.wordid
WHERE w.lemma <> 'coder'
AND w2.lemma = 'coder'
GROUP BY w.lemma
ORDER BY s.synsetid
The following code is used in a query for fetching records. It uses the electors.ID to find the corresponding voting_intention.elector from a second table.
$criteria = "FROM voting_intention,electors WHERE voting_intention.elector = electors.ID AND voting_intention.pledge IN ('C','P') AND electors.postal_vote = 1 AND electors.telephone > 0"
The problem is that some electors will have more than one pledge in the voting_intentions table.
I need it to match only on the latest voting_intention.pledge based on the field votin_intention.date for each elector.
What is the simplest way of implementing that.
The rest of the code:
function get_elector_phone($criteria){
$the_elector = mysql_query("SELECT * $criteria ORDER BY electors.ID ASC"); while($row = mysql_fetch_array($the_elector)) {
echo $row['ID'].','; }
}
You could use a sub-select with the MAX() function. Add the following into your WHERE clause.
AND voting_intention.date = (select MAX(date)
from voting_intention vote2
where voting_intention.elector = vote2.elector)
Here is a SQLFiddle of the results.
So pretty much, you only want to bother looking at the most recent row that fits the first two criteria in your code. In that case, you would want to filter out the voting_intention table beforehand to only have to worry about the most recent entries of each. There's a question/answer that shows how do do that here.
Try selecting the following instead of voting_intention (from the answer of the linked question, some table and field names replaced):
SELECT voting_intention.*
FROM voting_intention
INNER JOIN
(
SELECT elector, MAX(date) AS MaxDate
FROM voting_intention
GROUP BY elector
) groupedintention ON voting_intention.elector = groupedintention.elector
AND voting_intention.date = groupedintention .MaxDate
Question url: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?