This question already has answers here:
How to resolve ambiguous column names when retrieving results?
(11 answers)
Closed 2 years ago.
SELECT *
FROM companies
LEFT JOIN (ownership_reseller_to_company, users)
ON (users.ID=ownership_reseller_to_company.RESELLER_ID AND ownership_reseller_to_company.COMPANY_ID=companies.ID)
WHERE companies.NAME LIKE '%".$search_parm."%' AND users.ID='".$_USERID."'
My issue is I have NAME under companies and NAME under users but when I use $row['NAME']; I get the NAME under users. Is there a way to get the one under companies? I have tried $row['companies.NAME'] but with no success.
You need to use alias because column name is same in both table.
Example:
SELECT companies.name as cName, users.name as uName FROM companies .......
Than use like:
$row['cName'] // company name
$row['uName'] // user name
if you are using PDO you can:
$pdo->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, 1);
from the docs (http://www.php.net/manual/en/pdo.constants.php):
Prepend the containing table name to each column name returned in the result set. The table name and column name are separated by a decimal (.) character. Support of this attribute is at the driver level; it may not be supported by your driver.
Related
This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I have two tables on a forum that I am referencing via a SELECT query. One table is the phpBB_users table, which holds the basic data for a member while the second table, phpbb_profile_fields_data, holds details such as address, email, phone, etc. Both tables use user_id as the key. My form selects the $smode parameter, which lists the various column headings to search for and the $parameter variable, which enters the actual data to search for. The results are displayed in an HTML table.
Here's my code:
$conn = new mysqli('localhost',$user,$pass,$database);
$sql = "SELECT * FROM phpbb_users
INNER JOIN phpbb_profile_fields_data ON phpbb_users.user_id = phpbb_profile_fields_data.user_id
WHERE $smode = $parameter
ORDER BY username";
$result = $conn->query($sql);
If I select group_id (which is in the phpbb_users table) as my $smode variable and enter a given group number as the $parameter variable, I get a nice listing of the members within that group. If I change the $smode variable to pf_user_lastname (which is in the phpbb_profile_fields_data table) and enter a common last name I don't get any results. The same holds true if I use username as the $smode variable (which is in the phpbb_users table). I get no results. The group_id column is an integer while the other two are alphanumeric variables but when I change the $smode to pf_mh_year, which is a 4 digit integer in the phpbb_profile_fields_data table, I still don't get any results. I get no error messages in the error_log because $result->num_rows is zero - except for the first case where the listing displays.
I did echo the $sql and it looked fine. But when I tested it in PHPMyAdmin like you suggested it showed that I needed to place the $parameter value in single quotes. Not sure why it worked without them for the one integer column but not the other but either way, everything works now that the quotes are in place.
This question already has answers here:
How to delete duplicates on a MySQL table?
(25 answers)
Closed 3 years ago.
I have a SQL Tabel with three column id, Username, and Video I need a SQL query to remove all duplicates thanks
table SS link https://ibb.co/wC4p7kS
Since your screenshot is of phpMyAdmin, I'm going to assume that we're talking about MySQL here (when asking questions, that's helpful information as various SQL dialects have different tools that can be used).
If you don't need id in your results, you can just use DISTINCT:
SELECT
DISTINCT
Username,
Video
FROM
YourTableName
This returns a list of distinct rows (meaning it considers all columns in determining what is distinct, which is why including id here won't work).
If you do need to return an ID, you would have to use GROUP BY:
SELECT
MIN(id) AS id,
Username,
Video
FROM
YourTableName
GROUP BY
Username,
Video
Obviously from there, you could select a different ID (MAX(id) for example), or you could select a list of IDs using GROUP_CONCAT (e.g. GROUP_CONCAT(id ORDER BY id ASC) AS id, which will give you a comma-separated list by default unless you change it)
It can also be done using window/analytic functions, which can be a value in very large tables.
Note: When you say "remove all duplicates", I'm assuming you mean from your results. If you mean literally delete them from the table, you could use this:
DELETE FROM
YourTableName
WHERE
id NOT IN(SELECT MIN(id) FROM YourTableName GROUP BY Username,Video)
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.
My query is:
select
t.*,
e.emp_name,
d.department_name
from
task as t
LEFT JOIN employee as e on(e.emp_id=t.assign_to)
LEFT JOIN department as d on(d.depart_id=e.depart_id)
where
t.task_status='PENDING'
AND t.created_by!='31'
AND t.assign_to IN ('31')
order by
t.task_id DESC
The stored value in database
IN doesn't work like that
Example if your data looks like:
ManagerName, ManagerOf
'John', 'Steve,Sarah,Dave'
You can't do:
SELECT * FROM managers WHERE 'sarah' IN ManagerOf
IN is best conceived as an extension of OR:
SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
--is the same as:
SELECT * FROM managers WHERE
managerof = 'Sarah' OR
managerof = 'Steve'
There would be as many OR clauses as there are items in the IN list.
Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then
There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest
You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query
This question already has answers here:
How to remove new line characters from data rows in mysql?
(9 answers)
Closed 6 years ago.
I have a table Types that has the following columns: ID, Name, Type. The table is filled with about 300 rows. One of the rows:
ID Name Type
------------------
1 BMW S 1000 RR
The following query returns this row:
SELECT * FROM Types WHERE Name = 'BMW'
However, the following query returns nothing:
SELECT * FROM Types WHERE Type = 'S 1000 RR'
There are no extra spaces in the Type, and the data types of Name and Type are exactly the same (varchar 255, utf8_unicode_ci). What can possibly cause this?
I am using MySQL, InnoDB. Using phpMyAdmin I get the exact same results, so no typo's in column names...
I've found the problem: to fill the table I am reading a textfile per line. The newline character was the problem, it is invisible in phpMyAdmin's browse table view, but I saw it when editing a single row.
The following query fixed my problem:
UPDATE Types SET Type = REPLACE(REPLACE(Type, '\r', ''), '\n', '');
Found in How to remove new line characters from data rows in mysql?
Thanks everyone for your comments.
I am sure that's due to trim
Try this
SELECT * FROM Types WHERE TRIM(Type) = 'S 1000 RR'
Its not like that. Your table name and column names are all good and the query is also giving correct o/p:
go
CREATE TABLE Types
(
ID int,
Name varchar(3),
Type varchar(9));
go
INSERT INTO Types
(ID, Name, Type)
VALUES
(1, 'BMW', 'S 1000 RR')
SELECT * FROM Types WHERE Type = 'S 1000 RR'
This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
Closed 9 years ago.
I have a database which has a field containing some comma separated values like 1,8,3,54,5,19,9..... I want to select only those rows where 2 doesn't exists.
The query below is used for finding all fields containing the number 2 in the attachedCompanyIDs column. However, I want to find all rows where that number doesn't exist, but I don't know how to use find_in_set in this case. Can any one please help me?
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs);
SELECT name FROM company
WHERE orderID = 1 AND NOT FIND_IN_SET(2, attachedCompanyIDs);
or
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs) = 0;