<?php
$sql = mysql_query("
SELECT navn FROM member_film WHERE username = '$pusername'
UNION SELECT meetup FROM member_meetups WHERE byusername = '$pusername'
UNION SELECT title FROM member_tutorials WHERE username = '$pusername'");
$rowit = mysql_fetch_array($sql);
$number = mysql_num_rows($sql);
?>
<? echo $number; ?>
<? echo $rowit["navn"]; ?><br>
<? echo $rowit["meetup"]; ?><br>
So i have this.
Now, i have 3 columns where username is username in the database.
2 in member_film, 1 in member_meetups
The echo $number, shows it correct, that i have 3.
Now when i try to echo the "navn", it echo only 1, when there is 2 columns?
And when i try to echo "meetup" it says undefined index: meetup, like there's nothing to show..
but there is, mysql_num_rows founded it, else it would have been 2?
I think most of you misunderstood me..
I want to show what the user($pusername) have been posted, in member_film, member_meetups, member_tutorials.. And then if e.g the user have 2 columns with his username, i want to display the "navn" from the same column. Same with title on member_tutorials, and meetup on member_meetups.
An example on how i want it to look like:
Your posts: (which as i want, checks the three table if there's any columns with the user's username in "username" column.)
My new video, its so good (navn, which is from member_film)
My boring meetup with my parents (title, which is from member_meetup)
My great great video!(navn, which is from member_film)
UNION selects, are joining select statements into 1 result set, therefor have the same column names.
Correct SQL:
SELECT navn colName, 'member_film' tableName FROM member_film WHERE username = '$pusername'
UNION SELECT meetup colName, 'member_meetups' tableName FROM member_meetups WHERE byusername = '$pusername'
UNION SELECT title colName, 'member_tutorials' tableName FROM member_tutorials WHERE username = '$pusername'");
and in the php:
<? echo $rowit["colName"]; ?><br>
and to find out what table:
<? echo $rowit["tableName "]; ?><br>
A union query combines the results of several queries into one resultset. In order for the union to work all subqueries have to return the fields with the same data types for each position of the query or each named field.
So your original query will result in a result set with one field called "navn" (taken from the first sub query of the union). So to make it more readable you could either rename the field using As myName on each of the subqueries (sam munkes' solution).
However, from the way you are accessing the query I believe you want to have one row with all 3 values on it then you should try using this instead:
SELECT member_film, member_meetups.meetup, meber_tutorials.title
FROM member_film, member_meetups, member_tutorials
WHERE member_film.username = '$pusername' AND
member_film.username = member_meetups.byusername AND
member_film.username = member_tutorials.username
Edit after the comments:
SELECT CONVERT(varchar(100), navn) AS Comment FROM member_film WHERE username = '$pusername'
UNION
SELECT CONVERT(varchar(100), meetup) AS Comment FROM member_meetups WHERE byusername = '$pusername'
UNION
SELECT CONVERT(varchar(100), title) AS Comment FROM member_tutorials WHERE username = '$pusername'
This should now yield a table having one column named 'comment' and being of varchar(100). You can now access all values via the comment column. If you need to know from which table the information is taken, use Sam's answer. I inserted the convert to ensure that all entries are of the same data type and length, if they are in you DB you can leave this portion out and just use the names. If needed the length needs to be adjusted.
To make it clear, there is only one column with the name "comment" now.
Related
I am trying to update a null column using another tables value but it doesn't seems to work right. below codes were tried
SET
"Test name "= "Test"(
SELECT Transformertest.Test,Transformertest.TestID
FROM public.Transformertest WHERE TestID='Tes3')
WHERE test2table.Type='Oil Immersed Transformers'
UPDATE
public.test2table
SET
"Test name" = subquery."Test"
FROM
(
SELECT
"Test"
FROM Transformertest WHERE "TestID"='Tes2'
) AS subquery
WHERE
"Type"='Auto Transformer' AND "Phase"='3' AND "Rated Frequency"='60';
enter image description here
don't use space in column name.
Integers don't need to be quoted
See the result here (enter link description here)
what you need to do here (assuming Phase and Rated Frequency are integers)
remove unnecessary "" and spaces on column names
UPDATE
public.test2table
SET
test_name = subquery.Test
FROM
(
SELECT
test
FROM Transformertest WHERE Test_ID='Tes2'
) AS subquery
WHERE
Type='Auto Transformer' AND Phase=3 AND Rated_Frequency=60;
this should be working now
I have a table with a nested field externalIds.value
I want to run a query where 2 entries match values in this field. Here is the query I have, which doesn't return any data.
SELECT keyField, field2, example.field1, example.field2,
externalIds.type, externalIds.value FROM (FLATTEN([dataset1.table1], externalIds))
WHERE
externalIds.value = '157' AND
externalIds.value = 'Some test data'
;
If I run this query with only 1 WHERE clause (externalIds.value = '157') and then run a query WHERE keyField = "The value returned from the previous query", then I get two lines. One showing the externalIds.value as '157' and another result where it is 'Some test data'.
I'm not interested in displaying both values in the result. My priority is to get the keyField WHERE the .value is '157' AND 'Some test data'
Maybe something like that:
SELECT keyField, field2, example.field1, example.field2,
externalIds.type, externalIds.value FROM [dataset1.table1]
OMIT RECORD IF NOT(
SOME(externalIds.value = '157') AND
SOME(externalIds.value = 'Some test data'))
I am using Postgres 9.3.2 to make a database of contacts.
Example: If i have a row in my table that looks something like this.
{
firstName : "First name"
lastName : "Last name"
emails : ["email#one.com", "email#two.com", "email#three.com]
}
PS: firstName, lastName and emails are columns in my db and the value associated is the value for that column for that specific row.
I want to be able to query the db so that if i query for the email "email#four.com" the result is nothing but if i query for "email#two.com" the result will be the above row entry.
I dont think the query
"Select * from contactTable where emails="email#two.com""
will work. instead i want to do something like
"Select * from contactTable where emails contains "email#two.com""
any ideas on how to do this?
"Select * from contactTable where emails contains "email#two.com""
I think you want:
"Select * from contactTable where thejsonfield -> emails
Example setup, after fixing up your totally broken json:
CREATE TABLE contacts AS SELECT '{
"firstName" : "First name",
"lastName" : "Last name",
"emails" : ["email#one.com", "email#two.com", "email#three.com"]
}'::json AS myjsonfield;
The following will work in PostgreSQL 9.4, but unfortunately does not in 9.3 due to the oversight of the missing json_array_elements_text function:
select *
from contacts,
lateral json_array_elements_text(myjsonfield -> 'emails') email
where email = 'email#two.com';
For 9.3, you have to use a clumsier method to scan the json array for matching values:
select *
from contacts,
lateral json_array_length(myjsonfield -> 'emails') numemails,
lateral generate_series(0, numemails) n
WHERE json_array_element_text(myjsonfield -> 'emails', n) = 'email#two.com';
You can't use the simple IN or = ANY constructs because (at this point) PostgreSQL doesn't understand that you might have a json array, so it'll fail with:
regress=> SELECT * FROM contacts WHERE 'email#two.com' = ANY (myjsonfield->'emails');
ERROR: op ANY/ALL (array) requires array on right side
LINE 1: SELECT * FROM contacts WHERE 'email#two.com' = ANY (myjsonfi...
^
as it expects a PostgreSQL array, not a json array, and there's no convenient builtin to turn a json array into a PostgreSQL array yet.
Postgres has support for parsing JSON. Here is documentation: http://www.postgresql.org/docs/9.3/static/functions-json.html. I can't give you more detailed answer since you didn't provide exact data and schema, but it's easy to find the right function in documentation.
I have an XML data type column called "tags".
In that, I am storing a collection, like so:
<ArrayOfString>
<string>personal</string>
<string>travel</string>
<string>gadgets</string>
<string>parenting</string>
</ArrayOfString>
I want to select all the rows, that have one of the values that I am looking for: for example, I want to select all rows in the table that have a tag "travel".
I know that this works, if I know the index of the value I am looking for:
select * from posts
where tags.value('(/ArrayOfString/string)[1]', 'nvarchar(1000)') = 'travel'
but this query works only if the tag "travel" is the 2nd item in the nodes. How do I check if a value exists, irrespective of the position it is in?
select *
from tags
where tags.exist('/ArrayOfString/string[. = "travel"]') = 1
Or like this if you want to check against a variable.
declare #Val varchar(10)
set #Val = 'travel'
select *
from tags
where tags.exist('/ArrayOfString/string[. = sql:variable("#Val")]') = 1
You can try something like this:
SELECT
*
FROM
dbo.Posts
WHERE
tags.exist('/ArrayOfString/string/text()[. = "travel"]') = 1
This will list all the rows that have "travel" in one of the strings in your XML
Morning All,
Im not to sure how i need to solve my following query... I have the following query which pulls back the desired records in SQL server...
SELECT agenda.AgendaItemNumber,Agenda.AgendaName, AgendaType.AgendaTypeDescription, userdetails.fullName
FROM Agenda
JOIN AgendaType ON AgendaType.AgendaTypeID=Agenda.AgendaTypeID
JOIN UserDetails ON Agenda.AgendaID = Userdetails.AgendaID
WHERE agenda.AgendaTypeID = '2'
AND AgendaItemNumber = AgendaItemNumber
AND AgendaName = AgendaName
AND AgendaTypeDescription = AgendaTypeDescription
AND AgendaItemNumber >= '3'
The above query works but i need to enhance this slightly. It pulls back the following results, which essentially are duplicate records except for the 'fullname' column...
What i would like to do is be able to add some extra code to this query so that when i run the query i am able to display one record for each 'AgendaItemNumber' and for it to concat both of the fullnames for this record. However i have additional AgendaItemsNumbers in this table that only have 1 x user fullname assigned to them. its just these few records within the image file i need to do something clever with.
Maybe there is a better way to complete this task?
Many thanks in advance. Any queries please dont hesitate to ask.
Regards
Betty
SELECT agenda.AgendaItemNumber,
Agenda.AgendaName,
AgendaType.AgendaTypeDescription,
STUFF(( SELECT ';' + FullName
FROM UserDetails
WHERE UserDetails.AgendaID = Agenda.AgendaID
FOR XML PATH('')
), 1, 1, '') AS fullName
FROM Agenda
INNER JOIN AgendaType
ON AgendaType.AgendaTypeID=Agenda.AgendaTypeID
INNER JOIN UserDetails
ON Agenda.AgendaID = Userdetails.AgendaID
WHERE agenda.AgendaTypeID = '2'
AND AgendaItemNumber = AgendaItemNumber
AND AgendaName = AgendaName
AND AgendaTypeDescription = AgendaTypeDescription
AND AgendaItemNumber >= '3'
ADENDUM
The XML extension in SQL-Server allows you to concatenate multiple rows into a single row. The actual intention of the extension is so you can output as XML (obviously), but there are some nifty tricks that are byproducts of the extensions. In the above query, if there were a column name in the subquery (FullName) it would output as <FullName>Joe Bloggs1</FullName><FullName>Joe Bloggs2</FullName>, because there is no column name it simply concatenates the rows (not forming proper XML). The PATH part allows you to specify an additional node, for example if you use PATH('Name') in the above you would get <Name>;Joe Bloggs</Name><Name>;Joe Bloggs2</Name> If you combine Path with a column name you would get Joe Bloggs.
Finally the STUFF just removes the semicolon at the start of the list.