How to JOIN tables in different databases in Kohana?
$tb_new = 'db_zaboo_feed.feed_' . $feed;
$ids = DB::query(Database::SELECT,
"SELECT d.fuid_id, d.user_id FROM db_zaboo.displays d
LEFT JOIN $tb_new f ON(d.fuid_id = f.uid)
WHERE d.user_id = (SELECT user_id FROM $tb_new GROUP BY user_id) AND f.uid IS NULL")->execute();
ERROR:
Database_Exception [ 1146 ]: Table 'db_zaboo.displays' doesn't exist [ SELECT d.fuid_id, d.user_id FROM db_zaboo.displays d LEFT JOIN db_zaboo_feed.feed_3 f ON(d.fuid_id = f.uid) WHERE d.user_id = (SELECT user_id FROM db_zaboo_feed.feed_3 GROUP BY user_id) AND f.uid IS NULL ]
This is not a Kohana specific question. It is a MySQL question.
For operating on multiple databases in the same query, you have to use table names with its database name. Such as:
SELECT * FROM database1.table1 WHERE database1.table1.id IN (SELECT table1_id FROM database2.table2)
Be careful: by literally writing your query, I am not sure if Kohana's query builder prevents SQL injection attacks as it would do by building the SQL query using the query builder's specific functions. I would prevent it using the function mysql_real_escape_string
To use a different database config group pass either the name or the config object to execute().
$result = $query->execute('config_name')
http://kohanaframework.org/3.0/guide/database/query/builder#executing
http://kohanaframework.org/3.0/guide/database/config#connection-settings
Related
Can somebody tell me how to select multiple tables with WHERE in the query?
$sql = "SELECT * FROM leerlingen, leraren WHERE voornaam= '$username' AND password= '$password'";
Because this query gives an error
SELECT table1.*,
table2.*
FROM table1
JOIN table2
ON table1.id = table2.table1_id
WHERE table1.field LIKE "Hello"
AND table2.field LIKE "World;
This query should do what you are asking: Selecting everything from two different tables having different conditions in the WHERE part.
As someone else said, try to always use explicit JOIN syntax as is more readable and self-explanatory.
Since the code you wrote does not make it clear if you did, I will strongly suggest you use some sort of input validation and escaping to prevent SQL Injection.
To use multiple tables in where clause you have to use join between those tables
select le.*,l.* from leerlingen le join leraren l on le.col=l.col
where l.col1=value --table leraren in where
and le.col1=value --table leerlingen in where
And then you can apply where filter on them
SELECT t1.column_names,
t2.column_names
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.t1_id
WHERE t1.voornam = $username AND t1.password = $password t2.field ='some value';
I have two tables user and discovery but i'm unable to query them using join in postgres and php. My query is
$query = 'SELECT discovery.id,user.avatar,user.name,user.city,user.country,
discovery.image,discovery.likes,discovery.pincount FROM discovery
WHERE (discovery.id=$1)
INNER JOIN user ON (discovery.user=user.id)';
$res = pg_query_params($query, array($_POST['discovery_id']));
I am getting error
syntax error at or near "."
I have a field user in discovery table and also a table named user.
Please try this
$query = 'SELECT d.id, u.avatar, u.name, u.city, u.country,
d.image, d.likes, d.pincount FROM "discovery" AS d
INNER JOIN "user" AS u ON (d.user = u.id) WHERE (d.id = $id)';
$res = pg_query_params($query, array($_POST['discovery_id']));
The issue occured due to user is a reserved word on postgressql (postgresql.org/docs/8.1/static/sql-keywords-appendix.html), The table name user without " is referred as a keyword
Try this query
$query = "SELECT discovery.id,user.avatar,user.name,user.city,user.country,
discovery.image,discovery.likes,discovery.pincount FROM discovery d1
INNER JOIN user d2 ON d1.user=d2.id WHERE d1.id='1'";
PHP
Tables are:
I have 2 tables, one of trips and the other is key table (index,type)
I want to receive all the names of the trips that the index of the trip type is 1 (output = Alexander)
I receive into variable "$Trip_Type" the user's choice and in addition I need to add to the query another condition of variable $Trip_Population, that has a key table for his values named "Population". How can I combine this in the query?
"Population" is a key table like "Types": 1. index, 2. Type. In addition there is a column "Population_Type" in table Trips. I need all in 1 query
I have this query and I need to add for this the Population condition:
$query = "SELECT trips.Trip_Num
FROM trips JOIN trip_types ON trips.Trip_Type = trip_types.Type
WHERE trip_types.type = " . $Trip_Type;
select t1.name
from trips t1
inner join types t2 on t1.type =t2.type
$sql=
"SELECT t.name
from trips t
JOIN types ty ON t.type = ty.type
WHERE ty.type = " . $Trip_Type;
SELECT a.name
from trips a
JOIN types b ON t.type = a.type
WHERE b.type ='$Trip_Type'
I assume you are use php code to execute this query
I got a query using query builder and that is assigned to $qb variable. It works fine both from PHP and from the DB. Now, I was trying to use that query as a subquery like below:
$subQuery = $qb->getQuery()->getSql();
$query = 'select res.some_name
from ('.$subQuery.') as res';
But I get the following exception:
Caused by Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such column: res.some_name
As Doctrine already converted the $qb to something like this where Doctrine converted the original SQL query. For instance, there was something called AS legalentity_name but it's showing AS name1:
select res.some_name from (SELECT o0_.id AS id0, o0_.name AS name1, b1_.id AS id2, b1_.display AS display3, m2_.id AS id4, m2_.total AS total5 FROM Invoice i3_ INNER JOIN CodeableItem c4_ ON i3_.id = c4_.id INNER JOIN MonetaryItem m2_ ON i3_.id = m2_.id AND (1=1) INNER JOIN LineItem l5_ ON c4_.id = l5_.codeableItem_id LEFT JOIN MonetaryItem m6_ ON l5_.id = m6_.id AND (1=1) LEFT JOIN PresetLineItem p7_ ON c4_.id = p7_.codeableItem_id LEFT JOIN MonetaryItem m8_ ON p7_.id = m8_.id AND (1=1) INNER JOIN OrgUnit o0_ ON c4_.legalentity_id = o0_.id AND (1=1) INNER JOIN monetaryitem_listitem m9_ ON m2_.id = m9_.monetaryitem_id INNER JOIN BWListItem b1_ ON b1_.id = m9_.bwlistitem_id AND (1=1) INNER JOIN BWList b10_ ON b1_.bwlist_id = b10_.id AND (1=1) WHERE b10_.type = 'Vendor' GROUP BY c4_.legalentity_id, b1_.active, b1_.attributes, b1_.display, b1_.created, b1_.updated, b1_.lft, b1_.lvl, b1_.rgt, b1_.root, b1_.id, b1_.orgunit_id, b1_.bwlist_id, b1_.parent_id, b1_.rootou_id, m2_.created, m2_.updated, m2_.subtotal, m2_.total, m2_.description, m2_.id, c4_.number, c4_.externalId, c4_.status, c4_.overriddenDuringApproval, i3_.invoiceDate, i3_.dueDate, i3_.poNumber, m2_.rootou_id, c4_.image_id, c4_.legalentity_id, c4_.creator, c4_.owner_id ORDER BY o0_.name ASC, b1_.display ASC) as res
My question is: how can I use the raw SQL from $subQuery? Any help would be really beneficial. Cheers!
First off, subqueries in DQL is not possible. See Selecting from subquery in DQL
Secondly, you are putting computed SQL from Doctrine Query Language (DQL) into a subquery. This does not work as the database cannot find the column due to DQL prefixing characters/numeric values to the columns.
This is so the entities can be mapped correctly when using DQL.
You will need to build the subquery NOT using the DQL language (stop using that query builder, not sure if there is one that builds raw SQL).
I am selecting the user's information from my MySQL database as shown below:
SELECT * FROM `Users` WHERE `$user_or_id` = ?
However, I would like to add an extra bit off information to the returned data. The extra bit of data is the total number of records in a table named 'Venues' where the rows' field, 'user_id' is the same as the 'id' field in the table, 'Users'.
Please can you tell me where I am going wrong with the following query? Here is the error I am receiving:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '*) FROM Users AS u INNER JOIN Venues AS v ON u.id =
v.user_id WHERE u.id = '' at line 1
SELECT u.*, v.count(*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
SELECT u.*, COUNT(v.*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
COUNT is a MySQL function, not a member of table v. Pass it an argument representing what you want to count-- in this case, v's rows.
It should be COUNT(v.*). Otherwise it's interpreted as "function count inside table V", which isn't valid.
Just use count(*) instead of v.count(*).