multiple sort fields on multiple tables without join sql - mysql

How to do multiple sort without Join on tables? I'm able to sort fine by audDt but I'm unable to do a secondary sort on siteSt. Here is my code:
$sorc451sql = "Select * FROM table1 ORDER BY audDt";
while ($sorc451row = mysqli_fetch_array($sorc451res)) {
$sorc451arycust[$i][6] = $sorc451row['gropID'];
$sorc452sql = "Select * FROM table2 WHERE gropID=".$sorc451arycust[$i][6];
while ($sorc452row = mysqli_fetch_array($sorc452res)) {
$sorc452arycust[$i][12] = $sorc452row['siteSt'];
}
}

Its a quick fix:
$sorc451sql = "Select t1.gropID AS gropID, t2.siteSt AS siteSt FROM table1 t1 JOIN table2 t2 ON t1.gropID=t2.gropId ORDER BY audDt, SiteSt";
while ($sorc451row = mysqli_fetch_array($sorc451res)) {
$sorc451arycust[$i][6] = $sorc451row['gropID'];
$sorc452arycust[$i][12] = $sorc452row['siteSt'];
}
It looks like table1 isn't used so the following looks sufficient as a query:
$sorc451sql = "Select gropID, siteSt FROM table2 ORDER BY audDt, SiteSt";

Related

SQL - How can I combine Select Statements?

Is there any way to apply Logical Operator -> OR between 2 Select statements?
I am open to another other solutions which satisfies above OR Condition.
If there is no data in 1st Select then goto next Select
Example
SELECT column1, column2
FROM table-name1 JOIN table-name2
ON column-name1 = column-name2
WHERE //all condition
OR
SELECT column3, column4
FROM table-name3 JOIN table-name4
ON column-name3 = column-name4
WHERE //all condition
Condition 1/ Query 1
async getData() {
try {
const data = await this._conn.query(`
select first_name.value_name,quiz_table.answer, windows,player,first_name.value_id,country_place,current_name, pet_name, marker, relations
from schema_name.plugin,schema_name.quiz_table,schema_name.first_name, schema_name.value_version, schema_name.relationss
where plugin.answer= quiz_table.answer
and quiz_table.windows=first_name.value_id
and marker is not null
and schema_name.value_version.value_id= schema_name.first_name.value_id
and schema_name.value_version.caste= schema_name.first_name.caste
and schema_name.value_version.value_name= schema_name.first_name.value_name
and schema_name.value_version.version_number= schema_name.first_name.version_number
and schema_name.relationss.value_id= schema_name.first_name.value_id
and schema_name.relationss.caste= schema_name.first_name.caste
and schema_name.relationss.value_name= schema_name.first_name.value_name
and schema_name.relationss.version_number= schema_name.first_name.version_number
and schema_name.quiz_table.windows= schema_name.first_name.value_id
and in_process='N'
}
OR
Condition 2/ Query 2 - Display data
select schema_name.relationss."relations", schema_name.quiz_table."answer", schema_name.quiz_table."windows", schema_name.quiz_table."in_process", schema_name.quiz_table."object_name", schema_name.quiz_table."processed_date", schema_name.quiz_table."player", schema_name.quiz_table."country_place", schema_name.tools."mesh_scope_note", schema_name.plugin."current_name", schema_name.plugin."pet_name"
from schema_name.quiz_table, schema_name.tools, schema_name.plugin, schema_name.relationss, schema_name.value_version
where (in_process = 'N'
and schema_name.quiz_table."windows" = schema_name.tools."value_id"
and schema_name.quiz_table."player" = schema_name.tools."language"
and schema_name.quiz_table."answer" = schema_name.plugin."answer"
and schema_name.relationss."language" = schema_name.quiz_table."player"
and schema_name.relationss."language" = schema_name.tools."language"
and schema_name.relationss."caste" = schema_name.tools."caste"
and schema_name.relationss."value_name" = schema_name.tools."value_name"
and schema_name.relationss."version_number" = schema_name.tools."version_number"
and schema_name.relationss."value_id" = schema_name.tools."value_id"
and schema_name.value_version."value_id" = schema_name.tools."value_id"
and schema_name.value_version."version_number" = schema_name.tools."version_number"
and schema_name.value_version."caste" = schema_name.tools."caste"
)
NOTE- In WHERE clause both Select has different conditions
A simple method is union all and exists -- well, assuming the two selects return the same column:
with cte as (
select . . .
)
select *
from cte
union all
select *
from t2
where not exists (select 1 from cte);

complex sql multiple table query

I need all records from the URA table, joined on dobavljac for name of the dobavljac and all records from DOSTAVNICA table based on dostavnica.ura_id WHERE EXIST ura_id Join on gradilista for the name of gradilista.
Query:
$sql = "SELECT
ura.id,
ura.id_dobavljac,
ura.broj_racuna,
dobavljaci.id_dobavljac,
dobavljaci.naziv as dnaziv,
dobavljaci.oib,
dobavljaci.adresa,
dostavnica.ura_id,
dostavnica.id_dostavnica,
dostavnica.id_gradilista,
gradilista.id,
gradilista.naziv
FROM ura
INNER JOIN ura ON ura.id = dostavnica.ura_id
LEFT JOIN ura ON ura.id_dobavljac = dobavljaci.id_dobavljac
LEFT JOIN dostavnica ON dostavnica.id_gradilista = gradilista.id
";
Table schema:
and this is what I expect:
Try this one : A brief table structure would have helped. But based on the col names you have in the query I have posted my answer.
SELECT
ura.id,
ura.id_dobavljac,
ura.broj_racuna,
dobavljaci.id_dobavljac,
dobavljaci.naziv as dnaziv,
dobavljaci.oib,
dobavljaci.adresa,
dostavnica.ura_id,
dostavnica.id_dostavnica,
dostavnica.id_gradilista,
gradilista.id,
gradilista.naziv
FROM ura
INNER JOIN dobavljac ON ura.id_dobavljac = dobavljaci.id_dobavljac
INNER JOIN dostavnica ON ura.id = dostavnica.ura_id
WHERE EXISTS
(SELECT 1 from gradilista WHERE dostavnica.id_gradilista = gradilista.id)

how do i get data from array depending on some conditions

I have tables T1 and T2. In T1 I have column cat_id having integer values in array seperated by commma, e.g.
row1=1; row2=1,41; row3=40,1
In T2 I have column id having single integer values, e.g.
row1=1; row2=40; row3=41; row4=0
Now depending on id selected from T2 I want data from T1.
My problem is how do I apply the condition in T1. I am using CodeIgniter. I have tried like this.
$this->db->select('*');
$this->db->where('status', 'yes');
$this->db->where('cat', '1');
$this->db->like('category_id', '1');
$this->db->or_like('category_id', ',1');
$this->db->or_like('category_id', '1,');
$this->db->get('T1')->result_array();
but the problem persists as for id = 1, if there is data in T1 as 51,5 it also get selected.
How do i solve this. thanks in advance
Try using FIND_IN_SET
SELECT
*
FROM T1
LEFT JOIN T2 on FIND_IN_SET(T2.id, T1.cat_id)
WHERE T1.status = 'yes' AND T2.cat = 1
If you think the extra records are coming you can use INNER JOIN instead of LEFT JOIN.
IN COdeigniter
$sql_query = "
SELECT
*
FROM T1
LEFT JOIN T2 on FIND_IN_SET(T2.id, T1.cat_id)
WHERE T1.status = 'yes' AND T2.cat = 1
";
$this->db->query($sql_query);
As to the above question,using in_array() also helps.
$this->db->select('cat_id');
$row = $this->db->get('T1')->result_array();
$list = array();
foreach($row as $ind=>$val){
$c_id = explode(',', $row['cat_id);
if(in_array('1', $c_id){
array_push($list, $val)
}
}

combing two SELECT statements into a JOIN

I have two tables: table_1: item_id, name ... table_2: image_id, item_id, url
The following function retrieves the 'urls' of the items with the '$name' that is passed in. I first need to get the 'item_id' from table_1 to use in the query. I only need to return the 'url's from table_2.
It currently works fine, but I feel it could be streamlined to be done in one query with a JOIN rather than two separate queries. I've tried using different 'JOIN's to make it work but I can't seem to get it right.
Here's the function as it stands now with the two queries...
function get_items( $name ) {
global $wpdb;
$sql1 = "SELECT `item_id` FROM `table_1` WHERE `name` = '$name'";
$results1 = $wpdb->get_var( $sql1 );
$sql1 = "SELECT `url` FROM `table_2` WHERE `item_id` = '$results1'";
$results2 = $wpdb->get_results( $sql1, ARRAY_A );
return $results2;
}
and here is the 'JOIN' that I tried implementing, but was unsuccessful (I've also switched it around and did a 'LEFT/RIGHT JOIN' as well resulting in the same result) ...
$sql1 = "SELECT `table_2`.`url`
FROM `table_2`
INNER JOIN `table_1`
ON `table_2`.`item_id` = `table_1`.`item_id`
WHERE `table_1`.`item_id` = '$name'";
Any advice on combining these two queries?
The problem with your query is this WHERE table_1.item_id = '$name', it should be the name not the item_id
SELECT b.url
FROM table1 a
INNER JOIN table2 b
ON a.item_id = b.item_id
WHERE a.name = '$name'
select url from table_2 where item_id IN (select item_id from table_1 where name = $name )

mySQL query help needed

I have two tables:
t1
------------------
inv_ID
inv_memID
inv_projID
t2
------------------
is_ID
is_msgID
is_contID
I need to get all t2.is_contID into an array where
inv_projID = 5
t2.is_msgID = t1.inv_ID and
t1.inv_memID = 1
Seem pretty straight forward but I'm stuck... Tried this:
SELECT t2.is_contID
INNER JOIN t1 ON (t1.inv_ID = t2.is_msgID)
FROM t2
WHERE t1.inv_projID = 5
AND t1.inv_memID = 1
What am I missing?
FROM comes before JOIN.
SELECT t2.is_contID
FROM t2
INNER JOIN t1 ON (t1.inv_ID = t2.is_msgID)
WHERE t1.inv_projID = 5
AND t1.inv_memID = 1
SQL is very fussy about the order of the keywords.
The correct order is:
SELECT
FROM
JOIN
WHERE
HAVING
GROUP BY
ORDER
LIMIT <<-- MySQL only, other DB's user other keywords in other places.