Been trying the following code
$notorm->table()->select("table.*, table2.column, table2.table3.column2");
from http://sql-cross-queries.freexit.eu/dibi/notorm to create a JOIN statement, but to no avail. Am I missing out on anything?
I have reports(timestamp, incident_id, location id), incident(incident_id, incident_desc, location_id), and location(location_id, location_name). I am trying to get timestamp, incident_name, and location_name by joining reports, incident, and location. So I used the following statement:
$notorm->reports()->select("reports.*, incident.incident_id, incident.location.location_id");
but it's not returning anything. The following statement, though:
$notorm->reports()->select("reports.*");
returns reports.
Try this, not 100% sure but worth a try.
$notorm->reports()->join("reports", "LEFT JOIN incident ON reports.incident_id = incident.incident_id")
->join("reports", "LEFT JOIN location ON reports.location_id = location.location_id")
->select("reports.timestamp, incident.incident_desc, location.location_name");
in your table definition, there must be a field name table2_id present
Related
I am working on a laravel project(Laravel 6.8). I have a locations table and a dashboards table. I am trying to build a query that will return all matching records from the dashboards table. Looking at another SO question(Laravel 5.4 Raw Join Query), I saw a similar need and tried to adjust the code to fit my needs, but it still doesn't work.
This is what I have right now.
$locations = DB::table('locations')
->selectRaw("site_name,
COUNT(DISTINCT client) as client_count,
GROUP_CONCAT(client_lob) as lobs,
COUNT(DISTINCT client_lob) as lob_count,
SUM(agent_workstations) as aw_sum,
SUM(production_support_workstations) as psw_sum,
locations.*" )
->leftJoin('dashboards', 'locations.id', '=', 'dashboards.location_id')
->whereNotNull('latitude')
->groupBy(['site_name'])
->orderBy('site_name', 'asc')
->get();
It returns only the data from locations without returning anything from dashboards. I can't see what is wrong with this query. Can anyone offer some advice?
Because you never select the dashboard's fields, try to select the dashboard's field like this:
$locations = DB::table('locations')
->selectRaw("site_name,
COUNT(DISTINCT client) as client_count,
GROUP_CONCAT(client_lob) as lobs,
COUNT(DISTINCT client_lob) as lob_count,
SUM(agent_workstations) as aw_sum,
SUM(production_support_workstations) as psw_sum,
locations.*, dashboards.column1, dashboards.column2" )
I have almost thousands of data to display for my reports and it makes my browser lags due to the heavy data. I think that my query is the real problem. How can I optimized my query? is there something that I should add in my query?
I am using Xampp which supports PHP7.
SELECT
`payroll_billed_units`.`allotment_code`,
`payroll_billed_units`.`category_name`,
`payroll_billed_units`.`ntp_number`,
`payroll_billed_units`.`activity`,
`payroll_billed_units`.`regular_labor`,
`payroll_sub`.`block_number`,
(SELECT
GROUP_CONCAT(DISTINCT `lot_number` SEPARATOR ', ')
FROM
`payroll_billed_units` `lot_numbers`
WHERE
`lot_numbers`.`allotment_code` = `payroll_billed_units`.`allotment_code`
AND `lot_numbers`.`category_name` = `payroll_billed_units`.`category_name`
AND `lot_numbers`.`ntp_number` = `payroll_billed_units`.`ntp_number`
AND `lot_numbers`.`activity` = `payroll_billed_units`.`activity`) AS `lot_numbers`,
(SELECT
COUNT(`billed`.`ntp_id`)
FROM
`regular_ntp` `billed`
WHERE
`billed`.`allotment_code` = `payroll_billed_units`.`allotment_code`
AND `billed`.`category_name` = `payroll_billed_units`.`category_name`
AND `billed`.`ntp_number` = `payroll_billed_units`.`ntp_number`
AND `billed`.`activity` = `payroll_billed_units`.`activity`) AS `billed`,
(SELECT
COUNT(`approved`.`id`)
FROM
`payroll_billed_units` `approved`
WHERE
`approved`.`allotment_code` = `payroll_billed_units`.`allotment_code`
AND `approved`.`category_name` = `payroll_billed_units`.`category_name`
AND `approved`.`ntp_number` = `payroll_billed_units`.`ntp_number`
AND `approved`.`activity` = `payroll_billed_units`.`activity`) AS `approved`
FROM
`payroll_billed_units`
JOIN payroll_transaction ON payroll_billed_units.billing_number =
payroll_transaction.billing_number
JOIN payroll_sub ON payroll_transaction.billing_number =
payroll_sub.billing_number
WHERE payroll_billed_units.billing_date = '2019-02-13'
AND payroll_transaction.contractor_name = 'Roy Codal' GROUP BY allotment_code, category_name, activity
I was expecting that it will load or display all my data.
The biggest problem are the dependendt sub-selects, they are responsible for a bad performance. A sub-select will be executed for EVERY ROW of the outer query. And if you cascade subs-selects, you'll quickly have a query run forever.
If any of the parts would yield only 5 resultsets, 3 sub-select would mean that the database has to run 625 queries (5^4)!
Use JOINs.
Several of your tables need this 'composite' index:
INDEX(allotment_code, category_name, ntp_number, activity) -- in any order
payroll_transaction needs INDEX(contractor_name), though it may not get used.
payroll_billed_units needs INDEX(billing_date), though it may not get used.
For further discussion, please provide SHOW CREATE TABLE for each table and EXPLAIN SELECT ...
Use simply COUNT(*) instead of COUNT(foo). The latter checks the column for being not-NULL before including it. This is usually not needed. The reader is confused by thinking that there might be NULLs.
Your GROUP BY is improper because it is missing ntp_number. Read about the sql_mode of ONLY_FULL_GROUP_BY. I bring this up because you can almost get rid of some of those subqueries.
Another issue... Because of the "inflate-deflate" nature of JOIN with GROUP BY, the numbers may be inflated. I recommend you manually check the values of the COUNTs.
i need to update a lot of db values, so i guess it's better to use a sql statement, maybe creating and uploading a php file and running it from time to time.
in my db i have 3 related tables, let's say
tableA_label
tableB_image
tableC_text
the relations are as follows:
tableaA_label.ImageID refers to tableB_image.ID
tableB_image.TextID refers to tableC_text.ID
my goal is:
update tableA_label.Name
tableA_label.Name = tableC_text.title
where
tableC_text.ID = tableB_image.TextID
and
tableB_image.ID = tableA_label.ImageID
.....
how can accomplish this using an sql statement?
thank you for supporting
Try this query:
UPDATE tableA_label SET
tableA_label.Name = (SELECT TableC_text.title FROM TableC_text INNER
JOIN TableB_image ON TableB_image.TextID = TableC_text.ID
WHERE TableB_image.ID = tableA_label.imageID)
$conn=odbc_connect('mydatabase','','');
$sql="SELECT
Orders.OrderID,
Orders.OrderDate,
\"Order Details\".OrderID,
\"Order Details\".UnitPrice,
\"Order Details\".Quantity,
\"Order Details\".Discount,
FROM
\"Order Details\"
INNER JOIN
Orders
ON \"Order Details\".OrderID = Orders.OrderID";
$rs=odbc_exec($conn,$sql) or die("<p>".odbc_errormsg());
while (($row = odbc_fetch_array($rs)) !== false)
When I try to output the results of $rs nothing is returned. If I try to access fields from either Orders or \"Order Details\" separately it works fine but if I try to JOIN the two tables it outputs nothing.
Is this the correct way to SELECT a field from a table that has a space in its name when using MSSQL? It seems to work when I try "SELECT * FROM \"Order Details\"" but when I try to join the tables I have to specify "\Order Details\".OrderID and I think that might be where it's getting confused.
Apache error log returns a strange "odbc_fetch_array(): No tuples available at this result" message, but I'm not positive this is the actual problem as I've seen this error message pop up randomly for unrelated reasons. Still, I don't quite understand it so thought I should mention it.
Thanks!
Instead of using "/", use brackets.
"SELECT Orders.OrderID, [Order Details].UnitPrice
FROM [Order Details] INNER JOIN Orders ON [Order Details].OrderID = Orders.OrderID"
With the help of this community, I was able to produce a query using an inner join which I thought would work for what I'm trying to do. Unfortunately, when I attempted to run the query found below, I received the following error:
ERROR: table name "bue" specified more than once
From what I've read on Google, some people have said that the "FROM bue" is not needed, but when I removed this, I got another error found below:
ERROR: syntax error at or near "INNER" at character 98
I'd very much appreciate your assistance in troubleshooting this. Thank you so very much.
Query:
UPDATE
bue
SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
bue
INNER JOIN
chapterassociation
ON bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
WHERE
bue.mbr_no IS NULL AND bue.chp_cd IS NULL
In PostgreSQL, specifying the table to be updated needs to be done only in the UPDATE clause, e.g. UPDATE bue. The FROM clause is only for additional tables referenced in the query. (If you were doing a self-join on bue, you would mention it again in the FROM clause, but you aren't in this case.)
The second error you get is likely just a simple syntax error. The other tricky thing is that JOIN/ON syntax doesn't fit in the FROM clause, so you have to move the join conditions to the WHERE clause. Try something like:
UPDATE
bue
SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
chapterassociation
WHERE
bue.mbr_no IS NULL AND bue.chp_cd IS NULL
AND bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
See http://www.postgresql.org/docs/current/interactive/sql-update.html.
(N.B. At least I don't know how to put JOIN/ON into an UPDATE statement... I could be missing something.)